Sha256: 4f091fab1bee80c41a75f77ad911b4a2c07337eae4fc246bfe636428140a333f

Contents?: true

Size: 1.25 KB

Versions: 19

Compression:

Stored size: 1.25 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_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
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
volt-0.9.1.pre4 lib/volt/reactive/reactive_accessors.rb
volt-0.9.1.pre3 lib/volt/reactive/reactive_accessors.rb
volt-0.9.1.pre2 lib/volt/reactive/reactive_accessors.rb
volt-0.9.1.pre1 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0.pre7 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0.pre6 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0.pre5 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0.pre4 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0.pre3 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0.pre2 lib/volt/reactive/reactive_accessors.rb
volt-0.9.0.pre1 lib/volt/reactive/reactive_accessors.rb
volt-0.8.27.beta9 lib/volt/reactive/reactive_accessors.rb
volt-0.8.27.beta8 lib/volt/reactive/reactive_accessors.rb
volt-0.8.27.beta7 lib/volt/reactive/reactive_accessors.rb
volt-0.8.27.beta6 lib/volt/reactive/reactive_accessors.rb
volt-0.8.27.beta5 lib/volt/reactive/reactive_accessors.rb
volt-0.8.27.beta4 lib/volt/reactive/reactive_accessors.rb
volt-0.8.27.beta3 lib/volt/reactive/reactive_accessors.rb