Sha256: c9d3b83ec213e6aa4e9d05b8608e72c9e74ee44d5ddbede59cc1a852e751c57f

Contents?: true

Size: 1.11 KB

Versions: 10

Compression:

Stored size: 1.11 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_reader(*names)
      names.each do |name|
        var_name = :"@#{name}"
        define_method(name.to_sym) do
          value = instance_variable_get(var_name)

          unless value
            value = ReactiveValue.new(nil)

            instance_variable_set(var_name, value)
          end

          value
        end
      end
    end

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

          if value
            value.cur = new_value
          else
            instance_variable_set(var_name, ReactiveValue.new(value))
          end
        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

10 entries across 10 versions & 1 rubygems

Version Path
volt-0.7.23 lib/volt/controllers/reactive_accessors.rb
volt-0.7.22 lib/volt/controllers/reactive_accessors.rb
volt-0.7.21 lib/volt/controllers/reactive_accessors.rb
volt-0.7.20 lib/volt/controllers/reactive_accessors.rb
volt-0.7.19 lib/volt/controllers/reactive_accessors.rb
volt-0.7.18 lib/volt/controllers/reactive_accessors.rb
volt-0.7.17 lib/volt/controllers/reactive_accessors.rb
volt-0.7.16 lib/volt/controllers/reactive_accessors.rb
volt-0.7.15 lib/volt/controllers/reactive_accessors.rb
volt-0.7.14 lib/volt/controllers/reactive_accessors.rb