Sha256: dedb40724d6ec0da5f7d3e34187115e914967e4aab8dad6601598ea636a86e15

Contents?: true

Size: 1.17 KB

Versions: 14

Compression:

Stored size: 1.17 KB

Contents

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_dependency_get(var_name)
      value_dep = instance_variable_get(:"@__#{var_name}_dependency")
      value_dep ||= instance_variable_set(:"@__#{var_name}_dependency", Dependency.new)
    end

    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

    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

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

  def self.included(base)
    base.send :extend, ClassMethods
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/reactive/reactive_accessors.rb
volt-0.8.13 lib/volt/reactive/reactive_accessors.rb
volt-0.8.11 lib/volt/reactive/reactive_accessors.rb
volt-0.8.10 lib/volt/reactive/reactive_accessors.rb
volt-0.8.9 lib/volt/reactive/reactive_accessors.rb
volt-0.8.8 lib/volt/reactive/reactive_accessors.rb
volt-0.8.7 lib/volt/reactive/reactive_accessors.rb
volt-0.8.6 lib/volt/reactive/reactive_accessors.rb
volt-0.8.5 lib/volt/reactive/reactive_accessors.rb
volt-0.8.4 lib/volt/reactive/reactive_accessors.rb
volt-0.8.3 lib/volt/reactive/reactive_accessors.rb
volt-0.8.2 lib/volt/reactive/reactive_accessors.rb
volt-0.8.1 lib/volt/reactive/reactive_accessors.rb
volt-0.8.0 lib/volt/reactive/reactive_accessors.rb