Skip to content

Commit

Permalink
Add DIDefaultsArchive and DIDefaultsArchiveSync protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
k06a committed May 29, 2016
1 parent 26426fb commit 4a5b642
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DeluxeInjection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "DeluxeInjection"
s.version = "0.3.0"
s.version = "0.3.1"
s.summary = "Simplest Objective-C Dependency Injection (DI:syringe:) implementation ever"

s.description = <<-DESC
Expand Down
12 changes: 10 additions & 2 deletions DeluxeInjection/Classes/DIDefaults.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// DIDefaults.h
// Pods
// DeluxeInjection
//
// Copyright (c) 2016 Anton Bukov <k06aaa@gmail.com>
//
Expand Down Expand Up @@ -29,7 +29,15 @@ NS_ASSUME_NONNULL_BEGIN

@end

@interface NSObject (DIDefaults) <DIDefaults, DIDefaultsSync>
@protocol DIDefaultsArchived <NSObject>

@end

@protocol DIDefaultsArchivedSync <NSObject>

@end

@interface NSObject (DIDefaults) <DIDefaults, DIDefaultsSync, DIDefaultsArchived, DIDefaultsArchivedSync>

@end

Expand Down
26 changes: 21 additions & 5 deletions DeluxeInjection/Classes/DIDefaults.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// DIDefaults.m
// Pods
// DeluxeInjection
//
// Copyright (c) 2016 Anton Bukov <k06aaa@gmail.com>
//
Expand All @@ -17,6 +17,7 @@
// limitations under the License.
//

#import <Foundation/Foundation.h>
#import "DIDeluxeInjectionPlugin.h"
#import "DIDefaults.h"

Expand All @@ -28,15 +29,22 @@ @implementation DeluxeInjection (DIDefaults)

#pragma mark - Private

+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock forProtocol:(Protocol *)protocol withSync:(BOOL)withSync {
+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock forProtocol:(Protocol *)protocol withSync:(BOOL)withSync withArchive:(BOOL)withArchive {
[self inject:^NSArray *(Class targetClass, SEL getter, SEL setter, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols) {
NSString *key = keyBlock(targetClass, propertyName, propertyClass, propertyProtocols) ?: propertyName;
return @[DIGetterMake(^id _Nullable(id _Nonnull target, id _Nullable __autoreleasing * _Nonnull ivar) {
if (withSync) {
[[NSUserDefaults standardUserDefaults] synchronize];
}
return [[NSUserDefaults standardUserDefaults] objectForKey:key];
id value = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if (withArchive && value) {
return [NSKeyedUnarchiver unarchiveObjectWithData:value];
}
return value;
}), DISetterWithOriginalMake(^(id _Nonnull target, id _Nullable __autoreleasing * _Nonnull ivar, id _Nonnull value, void (* _Nullable originalSetter)(id _Nonnull __strong, SEL _Nonnull, id _Nullable __strong)) {
if (withArchive && value) {
value = [NSKeyedArchiver archivedDataWithRootObject:value];
}
[[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
if (withSync) {
[[NSUserDefaults standardUserDefaults] synchronize];
Expand All @@ -57,8 +65,10 @@ + (void)injectDefaults {
}

+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock {
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaults) withSync:NO];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaultsSync) withSync:YES];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaults) withSync:NO withArchive:NO];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaultsSync) withSync:YES withArchive:NO];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaultsArchived) withSync:NO withArchive:YES];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaultsArchivedSync) withSync:YES withArchive:YES];
}

+ (void)rejectDefaults {
Expand All @@ -68,6 +78,12 @@ + (void)rejectDefaults {
[self reject:^BOOL(Class targetClass, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols) {
return YES;
} conformingProtocol:@protocol(DIDefaultsSync)];
[self reject:^BOOL(Class targetClass, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols) {
return YES;
} conformingProtocol:@protocol(DIDefaultsArchived)];
[self reject:^BOOL(Class targetClass, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols) {
return YES;
} conformingProtocol:@protocol(DIDefaultsArchivedSync)];
}

@end

0 comments on commit 4a5b642

Please sign in to comment.