rm-extensions 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/README.md +3 -1
- data/ext/NSObject+RMExtensions.h +161 -0
- data/ext/NSObject+RMExtensions.m +76 -0
- data/lib/motion/rm-extensions.rb +4 -25
- data/lib/rm-extensions/version.rb +1 -1
- data/lib/rm-extensions.rb +1 -0
- metadata +4 -2
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,9 @@ Extensions and helpers for dealing with various areas of rubymotion:
|
|
8
8
|
- retaining objects through async procedures
|
9
9
|
- weak attr_accessors
|
10
10
|
|
11
|
-
Currently depends on bubblewrap
|
11
|
+
Currently depends on bubblewrap.
|
12
|
+
|
13
|
+
AssociatedObject objc runtime taken from BlocksKit, modified to work with rubymotion.
|
12
14
|
|
13
15
|
## Installation
|
14
16
|
|
@@ -0,0 +1,161 @@
|
|
1
|
+
// This is a pull from BlocksKit, with modifications to accept an NSString,
|
2
|
+
// Since we can't send true unique pointers for objects from rubymotion, and
|
3
|
+
// we'll rely on sending Symbols from rubymotion which turn into unique strings
|
4
|
+
// in objective-c.
|
5
|
+
//
|
6
|
+
//
|
7
|
+
// NSObject+AssociatedObjects.h
|
8
|
+
// BlocksKit
|
9
|
+
//
|
10
|
+
|
11
|
+
/** Objective-C wrapper for 10.6+ associated object API.
|
12
|
+
|
13
|
+
In Mac OS X Snow Leopard and iOS 3.0, Apple introduced an addition to the
|
14
|
+
Objective-C Runtime called associated objects. Associated objects allow for the
|
15
|
+
pairing of a random key and object pair to be saved on an instance.
|
16
|
+
|
17
|
+
In BlocksKit, associated objects allow us to emulate instance variables in the
|
18
|
+
ategories we use.
|
19
|
+
|
20
|
+
Class methods also exist for each association. These associations are unique to
|
21
|
+
each class, and exist for the lifetime of the application unless set to `nil`.
|
22
|
+
Each class is a unique meta-object; the ultimate singleton.
|
23
|
+
|
24
|
+
Created by [Andy Matuschak](https://github.com/andymatuschak) as
|
25
|
+
`AMAssociatedObjects`.
|
26
|
+
*/
|
27
|
+
@interface NSObject (RMEXTAssociatedObjects)
|
28
|
+
|
29
|
+
/** Strongly associates an object with the reciever.
|
30
|
+
|
31
|
+
The associated value is retained as if it were a property
|
32
|
+
synthesized with `nonatomic` and `retain`.
|
33
|
+
|
34
|
+
Using retained association is strongly recommended for most
|
35
|
+
Objective-C object derivative of NSObject, particularly
|
36
|
+
when it is subject to being externally released or is in an
|
37
|
+
`NSAutoreleasePool`.
|
38
|
+
|
39
|
+
@param value Any object.
|
40
|
+
@param key A unique key pointer.
|
41
|
+
*/
|
42
|
+
- (void)rmext_associateValue:(id)value withKey:(NSString *)key;
|
43
|
+
|
44
|
+
/** Strongly associates an object with the receiving class.
|
45
|
+
|
46
|
+
@see associateValue:withKey:
|
47
|
+
@param value Any object.
|
48
|
+
@param key A unique key pointer.
|
49
|
+
*/
|
50
|
+
+ (void)rmext_associateValue:(id)value withKey:(NSString *)key;
|
51
|
+
|
52
|
+
/** Strongly, thread-safely associates an object with the reciever.
|
53
|
+
|
54
|
+
The associated value is retained as if it were a property
|
55
|
+
synthesized with `atomic` and `retain`.
|
56
|
+
|
57
|
+
Using retained association is strongly recommended for most
|
58
|
+
Objective-C object derivative of NSObject, particularly
|
59
|
+
when it is subject to being externally released or is in an
|
60
|
+
`NSAutoreleasePool`.
|
61
|
+
|
62
|
+
@see associateValue:withKey:
|
63
|
+
@param value Any object.
|
64
|
+
@param key A unique key pointer.
|
65
|
+
*/
|
66
|
+
- (void)rmext_atomicallyAssociateValue:(id)value withKey:(NSString *)key;
|
67
|
+
|
68
|
+
/** Strongly, thread-safely associates an object with the receiving class.
|
69
|
+
|
70
|
+
@see associateValue:withKey:
|
71
|
+
@param value Any object.
|
72
|
+
@param key A unique key pointer.
|
73
|
+
*/
|
74
|
+
+ (void)rmext_atomicallyAssociateValue:(id)value withKey:(NSString *)key;
|
75
|
+
|
76
|
+
/** Associates a copy of an object with the reciever.
|
77
|
+
|
78
|
+
The associated value is copied as if it were a property
|
79
|
+
synthesized with `nonatomic` and `copy`.
|
80
|
+
|
81
|
+
Using copied association is recommended for a block or
|
82
|
+
otherwise `NSCopying`-compliant instances like NSString.
|
83
|
+
|
84
|
+
@param value Any object, pointer, or value.
|
85
|
+
@param key A unique key pointer.
|
86
|
+
*/
|
87
|
+
- (void)rmext_associateCopyOfValue:(id)value withKey:(NSString *)key;
|
88
|
+
|
89
|
+
/** Associates a copy of an object with the receiving class.
|
90
|
+
|
91
|
+
@see associateCopyOfValue:withKey:
|
92
|
+
@param value Any object, pointer, or value.
|
93
|
+
@param key A unique key pointer.
|
94
|
+
*/
|
95
|
+
+ (void)rmext_associateCopyOfValue:(id)value withKey:(NSString *)key;
|
96
|
+
|
97
|
+
/** Thread-safely associates a copy of an object with the reciever.
|
98
|
+
|
99
|
+
The associated value is copied as if it were a property
|
100
|
+
synthesized with `atomic` and `copy`.
|
101
|
+
|
102
|
+
Using copied association is recommended for a block or
|
103
|
+
otherwise `NSCopying`-compliant instances like NSString.
|
104
|
+
|
105
|
+
@see associateCopyOfValue:withKey:
|
106
|
+
@param value Any object, pointer, or value.
|
107
|
+
@param key A unique key pointer.
|
108
|
+
*/
|
109
|
+
- (void)rmext_atomicallyAssociateCopyOfValue:(id)value withKey:(NSString *)key;
|
110
|
+
|
111
|
+
/** Thread-safely associates a copy of an object with the receiving class.
|
112
|
+
|
113
|
+
@see associateCopyOfValue:withKey:
|
114
|
+
@param value Any object, pointer, or value.
|
115
|
+
@param key A unique key pointer.
|
116
|
+
*/
|
117
|
+
+ (void)rmext_atomicallyAssociateCopyOfValue:(id)value withKey:(NSString *)key;
|
118
|
+
|
119
|
+
/** Weakly associates an object with the reciever.
|
120
|
+
|
121
|
+
A weak association will cause the pointer to be set to zero
|
122
|
+
or nil upon the disappearance of what it references;
|
123
|
+
in other words, the associated object is not kept alive.
|
124
|
+
|
125
|
+
@param value Any object.
|
126
|
+
@param key A unique key pointer.
|
127
|
+
*/
|
128
|
+
- (void)rmext_weaklyAssociateValue:(id)value withKey:(NSString *)key;
|
129
|
+
|
130
|
+
/** Weakly associates an object with the receiving class.
|
131
|
+
|
132
|
+
@see weaklyAssociateValue:withKey:
|
133
|
+
@param value Any object.
|
134
|
+
@param key A unique key pointer.
|
135
|
+
*/
|
136
|
+
+ (void)rmext_weaklyAssociateValue:(id)value withKey:(NSString *)key;
|
137
|
+
|
138
|
+
/** Returns the associated value for a key on the reciever.
|
139
|
+
|
140
|
+
@param key A unique key pointer.
|
141
|
+
@return The object associated with the key, or `nil` if not found.
|
142
|
+
*/
|
143
|
+
- (id)rmext_associatedValueForKey:(NSString *)key;
|
144
|
+
|
145
|
+
/** Returns the associated value for a key on the receiving class.
|
146
|
+
|
147
|
+
@see associatedValueForKey:
|
148
|
+
@param key A unique key pointer.
|
149
|
+
@return The object associated with the key, or `nil` if not found.
|
150
|
+
*/
|
151
|
+
+ (id)rmext_associatedValueForKey:(NSString *)key;
|
152
|
+
|
153
|
+
/** Returns the reciever to a clean state by removing all
|
154
|
+
associated objects, releasing them if necessary. */
|
155
|
+
- (void)rmext_removeAllAssociatedObjects;
|
156
|
+
|
157
|
+
/** Returns the recieving class to a clean state by removing
|
158
|
+
all associated objects, releasing them if necessary. */
|
159
|
+
+ (void)rmext_removeAllAssociatedObjects;
|
160
|
+
|
161
|
+
@end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
// This is a pull from BlocksKit, with modifications to accept an NSString,
|
2
|
+
// Since we can't send true unique pointers for objects from rubymotion, and
|
3
|
+
// we'll rely on sending Symbols from rubymotion which turn into unique strings
|
4
|
+
// in objective-c.
|
5
|
+
//
|
6
|
+
//
|
7
|
+
// NSObject+AssociatedObjects.m
|
8
|
+
// BlocksKit
|
9
|
+
//
|
10
|
+
|
11
|
+
#import "NSObject+RMExtensions.h"
|
12
|
+
#import <objc/runtime.h>
|
13
|
+
|
14
|
+
@implementation NSObject (RMEXTAssociatedObjects)
|
15
|
+
|
16
|
+
#pragma mark - Instance Methods
|
17
|
+
|
18
|
+
- (void)rmext_associateValue:(id)value withKey:(NSString *)key {
|
19
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
20
|
+
}
|
21
|
+
|
22
|
+
- (void)rmext_atomicallyAssociateValue:(id)value withKey:(NSString *)key {
|
23
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN);
|
24
|
+
}
|
25
|
+
|
26
|
+
- (void)rmext_associateCopyOfValue:(id)value withKey:(NSString *)key {
|
27
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY_NONATOMIC);
|
28
|
+
}
|
29
|
+
|
30
|
+
- (void)rmext_atomicallyAssociateCopyOfValue:(id)value withKey:(NSString *)key {
|
31
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY);
|
32
|
+
}
|
33
|
+
|
34
|
+
- (void)rmext_weaklyAssociateValue:(id)value withKey:(NSString *)key {
|
35
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_ASSIGN);
|
36
|
+
}
|
37
|
+
|
38
|
+
- (id)rmext_associatedValueForKey:(NSString *)key {
|
39
|
+
return objc_getAssociatedObject(self, key);
|
40
|
+
}
|
41
|
+
|
42
|
+
- (void)rmext_removeAllAssociatedObjects {
|
43
|
+
objc_removeAssociatedObjects(self);
|
44
|
+
}
|
45
|
+
|
46
|
+
#pragma mark - Class Methods
|
47
|
+
|
48
|
+
+ (void)rmext_associateValue:(id)value withKey:(NSString *)key {
|
49
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
50
|
+
}
|
51
|
+
|
52
|
+
+ (void)rmext_atomicallyAssociateValue:(id)value withKey:(NSString *)key {
|
53
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN);
|
54
|
+
}
|
55
|
+
|
56
|
+
+ (void)rmext_associateCopyOfValue:(id)value withKey:(NSString *)key {
|
57
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY_NONATOMIC);
|
58
|
+
}
|
59
|
+
|
60
|
+
+ (void)rmext_atomicallyAssociateCopyOfValue:(id)value withKey:(NSString *)key {
|
61
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY);
|
62
|
+
}
|
63
|
+
|
64
|
+
+ (void)rmext_weaklyAssociateValue:(id)value withKey:(NSString *)key {
|
65
|
+
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_ASSIGN);
|
66
|
+
}
|
67
|
+
|
68
|
+
+ (id)rmext_associatedValueForKey:(NSString *)key {
|
69
|
+
return objc_getAssociatedObject(self, key);
|
70
|
+
}
|
71
|
+
|
72
|
+
+ (void)rmext_removeAllAssociatedObjects {
|
73
|
+
objc_removeAssociatedObjects(self);
|
74
|
+
}
|
75
|
+
|
76
|
+
@end
|
data/lib/motion/rm-extensions.rb
CHANGED
@@ -3,30 +3,13 @@ module RMExtensions
|
|
3
3
|
# this module is included on Object, so these methods are available from anywhere in your code.
|
4
4
|
module ObjectExtensions
|
5
5
|
|
6
|
-
# get a pointer for a symbol, which can be used as a key.
|
7
|
-
# the hash lookup table is kept per instance and not global so as objects dealloc,
|
8
|
-
# these mappings get freed.
|
9
|
-
def rmext_pointer_for_symbol(sym)
|
10
|
-
@rmext_pointers_for_symbols ||= {}
|
11
|
-
@rmext_pointers_for_symbols[sym] ||= begin
|
12
|
-
ptr = Pointer.new(:object)
|
13
|
-
ptr.assign(sym)
|
14
|
-
ptr
|
15
|
-
end
|
16
|
-
@rmext_pointers_for_symbols[sym]
|
17
|
-
end
|
18
|
-
|
19
6
|
def rmext_weak_attr_accessor(*attrs)
|
20
7
|
attrs.each do |attr|
|
21
8
|
define_method(attr) do
|
22
|
-
|
23
|
-
# p "weak_getter", attr, ptr
|
24
|
-
associatedValueForKey(ptr)
|
9
|
+
rmext_associatedValueForKey(attr.to_sym)
|
25
10
|
end
|
26
11
|
define_method("#{attr}=") do |val|
|
27
|
-
|
28
|
-
# p "weak_setter", attr, ptr
|
29
|
-
weaklyAssociateValue(val, withKey:ptr)
|
12
|
+
rmext_weaklyAssociateValue(val, withKey: attr.to_sym)
|
30
13
|
val
|
31
14
|
end
|
32
15
|
end
|
@@ -35,14 +18,10 @@ module RMExtensions
|
|
35
18
|
def rmext_copy_attr_accessor(*attrs)
|
36
19
|
attrs.each do |attr|
|
37
20
|
define_method(attr) do
|
38
|
-
|
39
|
-
# p "copy_getter", attr, ptr
|
40
|
-
associatedValueForKey(ptr)
|
21
|
+
rmext_associatedValueForKey(attr.to_sym)
|
41
22
|
end
|
42
23
|
define_method("#{attr}=") do |val|
|
43
|
-
|
44
|
-
# p "copy_setter", attr, ptr
|
45
|
-
associateCopyOfValue(val, withKey:ptr)
|
24
|
+
rmext_associateCopyOfValue(val, withKey: attr.to_sym)
|
46
25
|
val
|
47
26
|
end
|
48
27
|
end
|
data/lib/rm-extensions.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rm-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bubble-wrap
|
@@ -39,6 +39,8 @@ files:
|
|
39
39
|
- LICENSE.txt
|
40
40
|
- README.md
|
41
41
|
- Rakefile
|
42
|
+
- ext/NSObject+RMExtensions.h
|
43
|
+
- ext/NSObject+RMExtensions.m
|
42
44
|
- lib/motion/rm-extensions.rb
|
43
45
|
- lib/rm-extensions.rb
|
44
46
|
- lib/rm-extensions/version.rb
|