Skip to content

Commit

Permalink
Merge pull request #15 from twotoasters/subclass-responsibility
Browse files Browse the repository at this point in the history
Subclass responsibility
  • Loading branch information
macdrevx committed Feb 3, 2014
2 parents 4bb67d3 + b7184b6 commit f34b263
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 3 deletions.
63 changes: 63 additions & 0 deletions Foundation/Error Utilities/TWTErrorUtilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// TWTErrorUtilities.h
// TWTToast
//
// Created by Prachi Gauriar on 12/6/2012.
// Copyright (c) 2013–2014 Prachi Gauriar.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

/*!
@header TWTErrorUtilities
@abstract Defines utility functions and categories for use when creating assertions and exceptions.
@discussion The utility functions allow for easily formatting selectors, method names, and exception reasons.
*/

#import <Foundation/Foundation.h>

/*!
@abstract Returns an NSString representation of the selector.
@discussion This simply returns the result of NSStringFromSelector(selector), prefixed with a '+' if receiver is
a class and '-' otherwise.
@param receiver The object responding to the selector.
@param selector The selector to which the receiver will be responding.
@result A pretty-printed NSString representation of the selector.
*/
extern NSString *TWTPrettySelector(id receiver, SEL selector);

/*!
@abstract Returns an NSString representation of the selector.
@discussion The string is of the form \@"+[receiver selector]" for class methods and \@"-[receiverClassName selector]"
for instance methods.
@param receiver The object responding to the selector.
@param selector The selector to which the receiver will be responding.
@result A pretty-printed NSString representation of the method name, including the receiving class.
*/
extern NSString *TWTPrettyMethodName(id receiver, SEL selector);

/*!
@abstract Returns an NSString that is formatted suitably for use as an exception message.
@discussion Formatting is consistent with Apple's own exception messages.
@param receiver The object responding to the selector. This is typically self.
@param selector The selector to which the receiver will be responding. This is typically _cmd.
@param format A format string describing the reason for the exception.
@result An NSString formatted suitable for use as an exception message.
*/
extern NSString *TWTExceptionString(id receiver, SEL selector, NSString *format, ...) NS_FORMAT_FUNCTION(3, 4);
51 changes: 51 additions & 0 deletions Foundation/Error Utilities/TWTErrorUtilities.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// TWTErrorUtilities.m
// TWTToast
//
// Created by Prachi Gauriar on 12/6/2012.
// Copyright (c) 2013–2014 Prachi Gauriar.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import "TWTErrorUtilities.h"

NSString *TWTPrettySelector(id receiver, SEL selector)
{
char methodType = [receiver isMemberOfClass:[receiver class]] ? '-' : '+';
return [NSString stringWithFormat:@"%c%@", methodType, NSStringFromSelector(selector)];
}


NSString *TWTPrettyMethodName(id receiver, SEL selector)
{
char methodType = [receiver isMemberOfClass:[receiver class]] ? '-' : '+';
return [NSString stringWithFormat:@"%c[%@ %@]", methodType, NSStringFromClass([receiver class]), NSStringFromSelector(selector)];
}


NSString *TWTExceptionString(id receiver, SEL selector, NSString *format, ...)
{
va_list arguments;
va_start(arguments, format);
NSString *messageString = [[NSString alloc] initWithFormat:format arguments:arguments];
va_end(arguments);

return [NSString stringWithFormat:@"*** %@: %@", TWTPrettyMethodName(receiver, selector), messageString];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// NSException+TWTSubclassResponsibility.h
// TWTToast
//
// Created by Prachi Gauriar on 1/5/2014.
// Copyright (c) 2014 Prachi Gauriar.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import <Foundation/Foundation.h>

/*!
The TWTSubclassResponsibility category of NSException provides a convenience factory method for creating
exceptions when implementing a given method is a subclass's responsibility.
*/
@interface NSException (TWTSubclassResponsibility)

/*!
@abstract Creates and returns a new NSInternalInconsistencyException indicating that implementing
the method specified by the given receiver-selector pair is a subclass's responsibility.
@param receiver The object responding to the selector. This is typically self.
@param selector The selector to which the receiver will be responding. This is typically _cmd.
@result A new NSInternalInconsistencyException
*/
+ (instancetype)twt_subclassResponsibilityExceptionWithReceiver:(id)receiver selector:(SEL)selector;

@end

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// NSException+TWTSubclassResponsibility.m
// TWTToast
//
// Created by Prachi Gauriar on 1/5/2014.
// Copyright (c) 2014 Prachi Gauriar.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import "NSException+TWTSubclassResponsibility.h"

#import "TWTErrorUtilities.h"

@implementation NSException (TWTSubclassResponsibility)

+ (instancetype)twt_subclassResponsibilityExceptionWithReceiver:(id)receiver selector:(SEL)selector
{
return [NSException exceptionWithName:NSInternalInconsistencyException
reason:TWTExceptionString(receiver, selector, @"subclasses must provide an implementation of this method")
userInfo:nil];
}

@end
20 changes: 17 additions & 3 deletions TWTToast.podspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
Pod::Spec.new do |s|
s.name = "TWTToast"
s.version = "0.1"
s.version = "0.2"
s.summary = "Tools and Utilities for Cocoa Development"
s.homepage = "https://github.com/twotoasters/Toast"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "Two Toasters" => "[email protected]" }
s.social_media_url = "http://twitter.com/twotoasters"
s.platform = :ios, '7.0'
s.source = { :git => "https://github.com/twotoasters/Toast.git", :tag => "0.1" }
s.source = { :git => "https://github.com/twotoasters/Toast.git", :tag => "0.2" }
s.requires_arc = true

## Subspec for Core files. Files that extend Foundation is an example
## Subspec for Core files
s.subspec 'Core' do |sp|
sp.source_files = "Core/**/*.{h,m}"
end

## Subspec for files related to Foundation
s.subspec 'Foundation' do |sp|
sp.source_files = "UIKit/**/*.{h,m}"

sp.subspec 'ErrorUtilities' do |ss|
ss.source_files = "Foundation/Error Utilities/*.{h,m}"
end

sp.subspec 'SubclassResponsibility' do |ss|
ss.dependency 'TWTToast/Foundation/ErrorUtilities'
ss.source_files = "Foundation/Subclass Responsibility/*.{h,m}"
end
end

## Subspec for Files Related to UIKit
s.subspec 'UIKit' do |sp|
sp.source_files = "UIKit/**/*.{h,m}"
Expand Down
36 changes: 36 additions & 0 deletions Toast.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
4C351FBD188495F4009AD246 /* UIColor+TWTColorHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C351FB9188495F4009AD246 /* UIColor+TWTColorHelpers.m */; };
4C351FBE188495F4009AD246 /* UIDevice+TWTSystemVersion.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C351FBC188495F4009AD246 /* UIDevice+TWTSystemVersion.m */; };
4C351FC118849803009AD246 /* TWTRandomizedTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C351FC018849803009AD246 /* TWTRandomizedTestCase.m */; };
4CFCDD6D189FF9C900A7C3F2 /* NSException+TWTSubclassResponsibility.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CFCDD6C189FF9C900A7C3F2 /* NSException+TWTSubclassResponsibility.m */; };
4CFCDD75189FFFB800A7C3F2 /* TWTErrorUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CFCDD74189FFFB800A7C3F2 /* TWTErrorUtilities.m */; };
A420E1431885023F0019E986 /* UIView+TWTConvenientConstraintAddition.m in Sources */ = {isa = PBXBuildFile; fileRef = A420E1421885023F0019E986 /* UIView+TWTConvenientConstraintAddition.m */; };
A43C03351884FEA9000F9753 /* TWTUIKitBlockSampleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A43C03341884FEA9000F9753 /* TWTUIKitBlockSampleViewController.m */; };
A43C03381884FEB6000F9753 /* TWTSampleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A43C03371884FEB6000F9753 /* TWTSampleViewController.m */; };
Expand Down Expand Up @@ -52,6 +54,10 @@
4C351FBC188495F4009AD246 /* UIDevice+TWTSystemVersion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+TWTSystemVersion.m"; sourceTree = "<group>"; };
4C351FBF18849803009AD246 /* TWTRandomizedTestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TWTRandomizedTestCase.h; sourceTree = "<group>"; };
4C351FC018849803009AD246 /* TWTRandomizedTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TWTRandomizedTestCase.m; sourceTree = "<group>"; };
4CFCDD6B189FF9C900A7C3F2 /* NSException+TWTSubclassResponsibility.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSException+TWTSubclassResponsibility.h"; sourceTree = "<group>"; };
4CFCDD6C189FF9C900A7C3F2 /* NSException+TWTSubclassResponsibility.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSException+TWTSubclassResponsibility.m"; sourceTree = "<group>"; };
4CFCDD73189FFFB800A7C3F2 /* TWTErrorUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TWTErrorUtilities.h; sourceTree = "<group>"; };
4CFCDD74189FFFB800A7C3F2 /* TWTErrorUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TWTErrorUtilities.m; sourceTree = "<group>"; };
A420E1411885023F0019E986 /* UIView+TWTConvenientConstraintAddition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+TWTConvenientConstraintAddition.h"; sourceTree = "<group>"; };
A420E1421885023F0019E986 /* UIView+TWTConvenientConstraintAddition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+TWTConvenientConstraintAddition.m"; sourceTree = "<group>"; };
A43C03331884FEA9000F9753 /* TWTUIKitBlockSampleViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TWTUIKitBlockSampleViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -140,6 +146,33 @@
path = Device;
sourceTree = "<group>";
};
4CFCDD69189FF9C900A7C3F2 /* Foundation */ = {
isa = PBXGroup;
children = (
4CFCDD72189FFFB700A7C3F2 /* Error Utilities */,
4CFCDD6A189FF9C900A7C3F2 /* Subclass Responsibility */,
);
path = Foundation;
sourceTree = "<group>";
};
4CFCDD6A189FF9C900A7C3F2 /* Subclass Responsibility */ = {
isa = PBXGroup;
children = (
4CFCDD6B189FF9C900A7C3F2 /* NSException+TWTSubclassResponsibility.h */,
4CFCDD6C189FF9C900A7C3F2 /* NSException+TWTSubclassResponsibility.m */,
);
path = "Subclass Responsibility";
sourceTree = "<group>";
};
4CFCDD72189FFFB700A7C3F2 /* Error Utilities */ = {
isa = PBXGroup;
children = (
4CFCDD73189FFFB800A7C3F2 /* TWTErrorUtilities.h */,
4CFCDD74189FFFB800A7C3F2 /* TWTErrorUtilities.m */,
);
path = "Error Utilities";
sourceTree = "<group>";
};
A420E140188502310019E986 /* Auto Layout */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -173,6 +206,7 @@
isa = PBXGroup;
children = (
A4D633D618838DC900DA51CB /* Core */,
4CFCDD69189FF9C900A7C3F2 /* Foundation */,
4C351FB6188495F4009AD246 /* UIKit */,
A4D6339E1883164000DA51CB /* ExampleApplication */,
A4D633B71883164000DA51CB /* ToastTests */,
Expand Down Expand Up @@ -420,12 +454,14 @@
A4D633EB1883916A00DA51CB /* main.m in Sources */,
A43C03401884FEF1000F9753 /* UIActionSheet+TWTBlocks.m in Sources */,
4C351FBD188495F4009AD246 /* UIColor+TWTColorHelpers.m in Sources */,
4CFCDD75189FFFB800A7C3F2 /* TWTErrorUtilities.m in Sources */,
A43C03381884FEB6000F9753 /* TWTSampleViewController.m in Sources */,
A420E1431885023F0019E986 /* UIView+TWTConvenientConstraintAddition.m in Sources */,
4C351FBE188495F4009AD246 /* UIDevice+TWTSystemVersion.m in Sources */,
A43C03351884FEA9000F9753 /* TWTUIKitBlockSampleViewController.m in Sources */,
A4D633EF18839EA900DA51CB /* TWTHighOrderFunctions.m in Sources */,
A4D633E31883914600DA51CB /* TWTAppDelegate.m in Sources */,
4CFCDD6D189FF9C900A7C3F2 /* NSException+TWTSubclassResponsibility.m in Sources */,
A43C03411884FEF1000F9753 /* UIAlertView+TWTBlocks.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit f34b263

Please sign in to comment.