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.



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

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



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

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

#reactive_reader(*names) ⇒ Object



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

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



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

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