Module: Volt::ReactiveAccessors::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#__reactive_dependency_get(var_name) ⇒ 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.



8
9
10
11
# File 'lib/volt/reactive/reactive_accessors.rb', line 8

def __reactive_dependency_get(var_name)
  value_dep = instance_variable_get(:"@__#{var_name}_dependency")
  value_dep ||= instance_variable_set(:"@__#{var_name}_dependency", Dependency.new)
end

#reactive_accessor(*names) ⇒ Object



37
38
39
40
# File 'lib/volt/reactive/reactive_accessors.rb', line 37

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

#reactive_reader(*names) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/volt/reactive/reactive_accessors.rb', line 13

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

      self.class.__reactive_dependency_get(name).depend

      value
    end
  end
end

#reactive_writer(*names) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/volt/reactive/reactive_accessors.rb', line 26

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

      self.class.__reactive_dependency_get(name).changed!
    end
  end
end