Module: RMExtensions::ObjectExtensions::Accessors

Defined in:
lib/motion/accessors.rb

Instance Method Summary collapse

Instance Method Details

#rmext_zeroing_weak_attr_accessor(*attrs) ⇒ Object Also known as: rmext_weak_attr_accessor

creates an attr_accessor like behavior, but the objects are stored in an NSMapTable with strong keys (the attrs) and weak values. If the value deallocates, it becomes nil, unlike a traditional WeakRef. does not conform to KVO like attr_accessor does.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/motion/accessors.rb', line 27

def rmext_zeroing_weak_attr_accessor(*attrs)
  attrs.each do |attr|
    define_method(attr) do
      if @__zeroing_weak_holders
        @__zeroing_weak_holders.objectForKey(attr)
      end
    end
    define_method("#{attr}=") do |val|
      @__zeroing_weak_holders ||= NSMapTable.strongToWeakObjectsMapTable
      @__zeroing_weak_holders.setObject(val, forKey:attr)
      val
    end
  end
end