rm-extensions 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/motion/rm-extensions.rb +21 -4
- data/lib/rm-extensions/version.rb +1 -1
- metadata +1 -1
data/lib/motion/rm-extensions.rb
CHANGED
@@ -3,14 +3,29 @@ 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
|
+
|
6
19
|
def rmext_weak_attr_accessor(*attrs)
|
7
20
|
attrs.each do |attr|
|
8
|
-
ptr = Pointer.new(:object)
|
9
|
-
ptr.assign(attr.to_sym)
|
10
21
|
define_method(attr) do
|
22
|
+
ptr = rmext_pointer_for_symbol(attr.to_sym)
|
23
|
+
# p "weak_getter", attr, ptr
|
11
24
|
associatedValueForKey(ptr)
|
12
25
|
end
|
13
26
|
define_method("#{attr}=") do |val|
|
27
|
+
ptr = rmext_pointer_for_symbol(attr.to_sym)
|
28
|
+
# p "weak_setter", attr, ptr
|
14
29
|
weaklyAssociateValue(val, withKey:ptr)
|
15
30
|
val
|
16
31
|
end
|
@@ -19,12 +34,14 @@ module RMExtensions
|
|
19
34
|
|
20
35
|
def rmext_copy_attr_accessor(*attrs)
|
21
36
|
attrs.each do |attr|
|
22
|
-
ptr = Pointer.new(:object)
|
23
|
-
ptr.assign(attr.to_sym)
|
24
37
|
define_method(attr) do
|
38
|
+
ptr = rmext_pointer_for_symbol(attr.to_sym)
|
39
|
+
# p "copy_getter", attr, ptr
|
25
40
|
associatedValueForKey(ptr)
|
26
41
|
end
|
27
42
|
define_method("#{attr}=") do |val|
|
43
|
+
ptr = rmext_pointer_for_symbol(attr.to_sym)
|
44
|
+
# p "copy_setter", attr, ptr
|
28
45
|
associateCopyOfValue(val, withKey:ptr)
|
29
46
|
val
|
30
47
|
end
|