Sha256: fa5e73676e8b7cdc239b6797b60255186acf53e8b8aba33e60f4fa8bcd44799d

Contents?: true

Size: 1.24 KB

Versions: 28

Compression:

Stored size: 1.24 KB

Contents

require 'volt/reactive/hash_dependency'

module Volt
  class ReactiveHash
    def initialize(values = {})
      @hash     = values
      @deps     = HashDependency.new
      @all_deps = Dependency.new
    end

    def ==(val)
      @all_deps.depend
      @hash == val
    end

    def blank?
      @hash.blank?
    end

    # TODO: We should finish off this class for reactivity
    def method_missing(method_name, *args, &block)
      @all_deps.depend
      @hash.send(method_name, *args, &block)
    end

    def [](key)
      @deps.depend(key)

      @hash[key]
    end

    def []=(key, value)
      @deps.changed!(key)
      @all_deps.changed!

      @hash[key] = value
    end

    def delete(key)
      @deps.delete(key)
      @all_deps.changed!
      @hash.delete(key)
    end

    def clear
      # Don't use .each_key so we get a clone here since we are
      # deleting as we go.
      @hash.keys.each do |key|
        delete(key)
      end

      @all_deps.changed!
    end

    def replace(hash)
      clear

      hash.each_pair do |key, value|
        self[key] = value
      end
    end

    def to_h
      @all_deps.depend
      @hash
    end

    def inspect
      @all_deps.depend
      "#<#{self.class.name} #{@hash.inspect}>"
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
volt-0.9.3 lib/volt/reactive/reactive_hash.rb
volt-0.9.3.pre6 lib/volt/reactive/reactive_hash.rb
volt-0.9.3.pre5 lib/volt/reactive/reactive_hash.rb
volt-0.9.3.pre4 lib/volt/reactive/reactive_hash.rb
volt-0.9.3.pre3 lib/volt/reactive/reactive_hash.rb
volt-0.9.3.pre2 lib/volt/reactive/reactive_hash.rb
volt-0.9.3.pre1 lib/volt/reactive/reactive_hash.rb
volt-0.9.2 lib/volt/reactive/reactive_hash.rb
volt-0.9.1 lib/volt/reactive/reactive_hash.rb
volt-0.9.1.pre5 lib/volt/reactive/reactive_hash.rb
volt-0.9.1.pre4 lib/volt/reactive/reactive_hash.rb
volt-0.9.1.pre3 lib/volt/reactive/reactive_hash.rb
volt-0.9.1.pre2 lib/volt/reactive/reactive_hash.rb
volt-0.9.1.pre1 lib/volt/reactive/reactive_hash.rb
volt-0.9.0 lib/volt/reactive/reactive_hash.rb
volt-0.9.0.pre7 lib/volt/reactive/reactive_hash.rb
volt-0.9.0.pre6 lib/volt/reactive/reactive_hash.rb
volt-0.9.0.pre5 lib/volt/reactive/reactive_hash.rb
volt-0.9.0.pre4 lib/volt/reactive/reactive_hash.rb
volt-0.9.0.pre3 lib/volt/reactive/reactive_hash.rb