Class: Volt::ReactiveHash

Inherits:
Object show all
Defined in:
lib/volt/reactive/reactive_hash.rb

Direct Known Subclasses

Errors

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ ReactiveHash

Returns a new instance of ReactiveHash.



5
6
7
8
9
# File 'lib/volt/reactive/reactive_hash.rb', line 5

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

TODO: We should finish off this class for reactivity



21
22
23
24
# File 'lib/volt/reactive/reactive_hash.rb', line 21

def method_missing(method_name, *args, &block)
  @all_deps.depend
  @hash.send(method_name, *args, &block)
end

Instance Method Details

#==(val) ⇒ Object



11
12
13
14
# File 'lib/volt/reactive/reactive_hash.rb', line 11

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

#[](key) ⇒ Object



26
27
28
29
30
# File 'lib/volt/reactive/reactive_hash.rb', line 26

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

  @hash[key]
end

#[]=(key, value) ⇒ Object



32
33
34
35
36
37
# File 'lib/volt/reactive/reactive_hash.rb', line 32

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

  @hash[key] = value
end

#blank?Boolean

Returns:



16
17
18
# File 'lib/volt/reactive/reactive_hash.rb', line 16

def blank?
  @hash.blank?
end

#clearObject



45
46
47
48
49
50
51
52
53
# File 'lib/volt/reactive/reactive_hash.rb', line 45

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

#delete(key) ⇒ Object



39
40
41
42
43
# File 'lib/volt/reactive/reactive_hash.rb', line 39

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

#inspectObject



72
73
74
75
# File 'lib/volt/reactive/reactive_hash.rb', line 72

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

#replace(hash) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/volt/reactive/reactive_hash.rb', line 55

def replace(hash)
  clear

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

#to_hObject



63
64
65
66
# File 'lib/volt/reactive/reactive_hash.rb', line 63

def to_h
  @all_deps.depend
  @hash
end

#to_jsonObject



68
69
70
# File 'lib/volt/reactive/reactive_hash.rb', line 68

def to_json
  @hash.to_json
end