Skip to content

Commit

Permalink
Merge bfa186e into 84abbec
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelengland committed Feb 12, 2015
2 parents 84abbec + bfa186e commit fb7e5e4
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 52 deletions.
4 changes: 4 additions & 0 deletions Classes/FlipTheSwitch/FlipTheSwitch.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
extern NSString *const FTSFeatureStatusChangedNotification;
extern NSString *const FTSFeatureStatusChangedNotificationFeatureKey;
extern NSString *const FTSFeatureStatusChangedNotificationEnabledKey;

@interface FlipTheSwitch : NSObject
- (instancetype)init __attribute__((unavailable("init not available ")));
+ (instancetype)sharedInstance;
Expand Down
31 changes: 27 additions & 4 deletions Classes/FlipTheSwitch/FlipTheSwitch.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#import "FlipTheSwitch.h"

NSString *const FTSFeatureStatusChangedNotification = @"FTSFeatureStatusChangedNotification";
NSString *const FTSFeatureStatusChangedNotificationFeatureKey = @"FTSFeatureStatusChangedNotificationFeatureKey";
NSString *const FTSFeatureStatusChangedNotificationEnabledKey = @"FTSFeatureStatusChangedNotificationEnabledKey";

@interface FlipTheSwitch ()
@property (nonatomic, readonly) NSUserDefaults *userDefaults;
@property (nonatomic, readonly) NSBundle *bundle;
@property (nonatomic, readonly) NSNotificationCenter *notificationCenter;
@end

@implementation FlipTheSwitch {
Expand All @@ -17,7 +22,8 @@ + (instancetype)sharedInstance
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] initWithUserDefaults:[NSUserDefaults standardUserDefaults]
bundle:[NSBundle mainBundle]];
bundle:[NSBundle mainBundle]
notificationCenter:[NSNotificationCenter defaultCenter]];
});
return sharedInstance;
}
Expand All @@ -26,13 +32,16 @@ + (instancetype)sharedInstance

- (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults
bundle:(NSBundle *)bundle
notificationCenter:(NSNotificationCenter *)notificationCenter
{
self = [super init];
if (self) {
NSParameterAssert(userDefaults);
NSParameterAssert(bundle);
NSParameterAssert(notificationCenter);
_userDefaults = userDefaults;
_bundle = bundle;
_notificationCenter = notificationCenter;
}
return self;
}
Expand All @@ -45,7 +54,7 @@ - (BOOL)isFeatureEnabled:(NSString *)feature
if (userEnabledFeature) {
return [userEnabledFeature boolValue];
} else {
return [[[self plistEnabledFeatures] objectForKey:feature] boolValue];
return [[self plistEnabledFeatures][feature] boolValue];
}
}

Expand All @@ -61,8 +70,12 @@ - (void)disableFeature:(NSString *)feature

- (void)setFeature:(NSString *)feature enabled:(BOOL)enabled
{
[self.userDefaults setBool:enabled forKey:[self userKeyForFeature:feature]];
[self.userDefaults synchronize];
if (![self isFeatureEnabled:feature] == enabled) {
[self.userDefaults setBool:enabled forKey:[self userKeyForFeature:feature]];
[self.userDefaults synchronize];
[self.notificationCenter postNotification:[self statusChangeNotificationForFeature:feature
enabled:enabled]];
}
}

#pragma mark - Private
Expand All @@ -85,4 +98,14 @@ - (NSString *)featurePlistPath
return [self.bundle pathForResource:@"Features" ofType:@"plist"];
}

- (NSNotification *)statusChangeNotificationForFeature:(NSString *)feature enabled:(BOOL)enabled
{
return [NSNotification notificationWithName:FTSFeatureStatusChangedNotification
object:self
userInfo:@{
FTSFeatureStatusChangedNotificationFeatureKey : feature,
FTSFeatureStatusChangedNotificationEnabledKey : @(enabled)
}];
}

@end
16 changes: 0 additions & 16 deletions Tests/FlipTheSwitchSpec.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
F803222F52BB5CB3482ED84D /* FlipTheSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = F8032766B6B64076F108F6DC /* FlipTheSwitch.m */; };
F803238AA8E0E1976F0F95DC /* FlipTheSwitchSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = F80322BF56E8D76FB9736847 /* FlipTheSwitchSpec.m */; };
F80325D46C885BC440A30C43 /* FlipTheSwitchSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = F80322BF56E8D76FB9736847 /* FlipTheSwitchSpec.m */; };
F803272CB8A5B38D4757608E /* GcovTestObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = F8032394DDE5CCE6C76898E9 /* GcovTestObserver.m */; };
F80329B3B762341EF7AD3E7F /* FlipTheSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = F8032766B6B64076F108F6DC /* FlipTheSwitch.m */; };
F80329FE8A8EF57B776A9FDE /* GcovTestObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = F8032394DDE5CCE6C76898E9 /* GcovTestObserver.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -58,9 +56,7 @@
CF8E94075C5A480F893ECEE0 /* Pods-FlipTheSwitchSpec-iOS.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlipTheSwitchSpec-iOS.xcconfig"; path = "Pods/Pods-FlipTheSwitchSpec-iOS.xcconfig"; sourceTree = "<group>"; };
F8032139C1E76BF6C51205CD /* FlipTheSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FlipTheSwitch.h; sourceTree = "<group>"; };
F80321987A56E7AF3BFF5FD9 /* FlipTheSwitchSpec-iOS-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FlipTheSwitchSpec-iOS-Prefix.pch"; sourceTree = "<group>"; };
F80322339C8E3439D05DCB0E /* GcovTestObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GcovTestObserver.h; sourceTree = "<group>"; };
F80322BF56E8D76FB9736847 /* FlipTheSwitchSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FlipTheSwitchSpec.m; sourceTree = "<group>"; };
F8032394DDE5CCE6C76898E9 /* GcovTestObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GcovTestObserver.m; sourceTree = "<group>"; };
F80325C7A88C24081CF78461 /* FlipTheSwitchSpec-iOS-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.info; path = "FlipTheSwitchSpec-iOS-Info.plist"; sourceTree = "<group>"; };
F80326FF993EEA2C7572BD21 /* FlipTheSwitchSpec-Mac-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FlipTheSwitchSpec-Mac-Prefix.pch"; sourceTree = "<group>"; };
F8032766B6B64076F108F6DC /* FlipTheSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FlipTheSwitch.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -156,15 +152,6 @@
path = Classes;
sourceTree = "<group>";
};
F80322747F4E4DC74EC73A3A /* Helpers */ = {
isa = PBXGroup;
children = (
F80322339C8E3439D05DCB0E /* GcovTestObserver.h */,
F8032394DDE5CCE6C76898E9 /* GcovTestObserver.m */,
);
path = Helpers;
sourceTree = "<group>";
};
F80323A5D7806CC7C7B8D36A /* FlipTheSwitch */ = {
isa = PBXGroup;
children = (
Expand All @@ -186,7 +173,6 @@
isa = PBXGroup;
children = (
F803205E2BED616EF1088A54 /* Classes */,
F80322747F4E4DC74EC73A3A /* Helpers */,
);
path = Spec;
sourceTree = "<group>";
Expand Down Expand Up @@ -354,7 +340,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F803272CB8A5B38D4757608E /* GcovTestObserver.m in Sources */,
F80325D46C885BC440A30C43 /* FlipTheSwitchSpec.m in Sources */,
F80329B3B762341EF7AD3E7F /* FlipTheSwitch.m in Sources */,
);
Expand All @@ -365,7 +350,6 @@
buildActionMask = 2147483647;
files = (
F803238AA8E0E1976F0F95DC /* FlipTheSwitchSpec.m in Sources */,
F80329FE8A8EF57B776A9FDE /* GcovTestObserver.m in Sources */,
F803222F52BB5CB3482ED84D /* FlipTheSwitch.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<EnvironmentVariables>
<EnvironmentVariable
key = "SPECTA_REPORTER_CLASS"
value = "GcovTestObserver"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5B9DCEAA193A869B00C63F9F"
BuildableName = "FlipTheSwitchSpec-Mac.xctest"
BlueprintName = "FlipTheSwitchSpec-Mac"
ReferencedContainer = "container:FlipTheSwitchSpec.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<EnvironmentVariables>
<EnvironmentVariable
key = "SPECTA_REPORTER_CLASS"
value = "GcovTestObserver"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5B87AFE9193A80F20082F7A1"
BuildableName = "FlipTheSwitchSpec-iOS.xctest"
BlueprintName = "FlipTheSwitchSpec-iOS"
ReferencedContainer = "container:FlipTheSwitchSpec.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
Expand Down
59 changes: 57 additions & 2 deletions Tests/Spec/Classes/FlipTheSwitch/FlipTheSwitchSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

@interface FlipTheSwitch (Spec)
- (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults
bundle:(NSBundle *)bundle;
bundle:(NSBundle *)bundle
notificationCenter:(NSNotificationCenter *)notificationCenter;
@end

SpecBegin(FlipTheSwitch)
Expand All @@ -15,12 +16,15 @@ - (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults

before(^{
userDefaults = [[NSUserDefaults alloc] init];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

standardFeature = @"standard_feature";
plistEnabledFeature = @"plist_enabled_feature";

subject = [[FlipTheSwitch alloc] initWithUserDefaults:userDefaults
bundle:[NSBundle bundleForClass:[self class]]];
bundle:bundle
notificationCenter:notificationCenter];
});

after(^{
Expand Down Expand Up @@ -85,4 +89,55 @@ - (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults
expect([subject isFeatureEnabled:plistEnabledFeature]).to.beTruthy();
});
});

describe(@"notifications", ^{
context(@"when feature already enabled", ^{
before(^{
[subject setFeature:standardFeature enabled:YES];
});

context(@"when feature re-enabled", ^{
it(@"does nothing", ^{
expect(^{ [subject setFeature:standardFeature enabled:YES]; }).toNot.notify(FTSFeatureStatusChangedNotification);
});
});

context(@"when feature disabled", ^{
it(@"sends a notification about the change", ^{
NSNotification *enabledNotification = [NSNotification notificationWithName:FTSFeatureStatusChangedNotification
object:subject
userInfo:@{
FTSFeatureStatusChangedNotificationFeatureKey : standardFeature,
FTSFeatureStatusChangedNotificationEnabledKey : @NO
}];
expect(^{ [subject setFeature:standardFeature enabled:NO]; }).to.notify(enabledNotification);
});
});
});

context(@"when feature already disabled", ^{
before(^{
[subject setFeature:standardFeature enabled:NO];
});

context(@"when feature re-disabled", ^{
it(@"does nothing", ^{
expect(^{ [subject setFeature:standardFeature enabled:NO]; }).toNot.notify(FTSFeatureStatusChangedNotification);
});
});

context(@"when feature enabled", ^{
it(@"sends a notification about the change", ^{
NSNotification *enabledNotification = [NSNotification notificationWithName:FTSFeatureStatusChangedNotification
object:subject
userInfo:@{
FTSFeatureStatusChangedNotificationFeatureKey : standardFeature,
FTSFeatureStatusChangedNotificationEnabledKey : @YES
}];
expect(^{ [subject setFeature:standardFeature enabled:YES]; }).to.notify(enabledNotification);
});

});
});
});
SpecEnd
4 changes: 0 additions & 4 deletions Tests/Spec/Helpers/GcovTestObserver.h

This file was deleted.

12 changes: 0 additions & 12 deletions Tests/Spec/Helpers/GcovTestObserver.m

This file was deleted.

0 comments on commit fb7e5e4

Please sign in to comment.