Sha256: f1c7edf79438c5855eb837816dd3e8dd8f7f8a6332db4ba40d5cacf9fe79f37f
Contents?: true
Size: 1.22 KB
Versions: 35
Compression:
Stored size: 1.22 KB
Contents
module Volt module ReactiveAccessors module ClassMethods # 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. def reactive_reader(*names) names.each do |name| var_name = :"@#{name}" define_method(name.to_sym) do value = instance_variable_get(var_name) __reactive_dependency_get(name).depend value end end end def reactive_writer(*names) names.each do |name| var_name = :"@#{name}" define_method("#{name}=") do |new_value| instance_variable_set(var_name, new_value) __reactive_dependency_get(name).changed! end end end def reactive_accessor(*names) reactive_reader(*names) reactive_writer(*names) end end def self.included(base) base.send :extend, ClassMethods end 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 end end
Version data entries
35 entries across 35 versions & 1 rubygems