Module: ReactiveAccessors::ClassMethods

Defined in:
lib/volt/controllers/reactive_accessors.rb

Instance Method Summary collapse

Instance Method Details

#reactive_accessor(*names) ⇒ Object



39
40
41
42
# File 'lib/volt/controllers/reactive_accessors.rb', line 39

def reactive_accessor(*names)
  reactive_reader(*names)
  reactive_writer(*names)
end

#reactive_reader(*names) ⇒ Object

Create a method to read a reactive value from an instance value. If it is not setup, create it so it can be updated through the reactive value at a later point.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/volt/controllers/reactive_accessors.rb', line 7

def reactive_reader(*names)
  names.each do |name|
    var_name = :"@#{name}"
    define_method(name.to_sym) do
      value = instance_variable_get(var_name)

      unless value
        value = ReactiveValue.new(nil)

        instance_variable_set(var_name, value)
      end

      value
    end
  end
end

#reactive_writer(*names) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/volt/controllers/reactive_accessors.rb', line 24

def reactive_writer(*names)
  names.each do |name|
    var_name = :"@#{name}"
    define_method(:"#{name}=") do |new_value|
      value = instance_variable_get(var_name)

      if value
        value.cur = new_value
      else
        instance_variable_set(var_name, ReactiveValue.new(value))
      end
    end
  end
end