Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/motion-weakattr.rb

Instance Method Summary collapse

Instance Method Details

#weak_attr(*attrs) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/motion/motion-weakattr.rb', line 2

def weak_attr(*attrs)
  attrs.each do |attr_name|
    attr_accessor attr_name

    normal_getter = "#{attr_name}"
    normal_setter = "#{attr_name}="
    with_getter = "#{attr_name}_with_weakref"
    without_getter = "#{attr_name}_without_weakref"
    with_setter = "#{attr_name}_with_weakref="
    without_setter = "#{attr_name}_without_weakref="

    define_method(with_setter) do |value|
      self.send(without_setter, WeakRef.new(value))
    end

    define_method(with_getter) do
      value = self.send(without_getter)
      if value.respond_to?(:weakref_alive?)
        return value if value.weakref_alive?
        nil
      else
        value
      end
    end

    alias_method without_getter, normal_getter
    alias_method normal_getter, with_getter

    alias_method without_setter, normal_setter
    alias_method normal_setter, with_setter
  end
end