Class: ReactiveGenerator

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

Class Method Summary collapse

Class Method Details

.find_reactives(object) ⇒ Object

Recursively loop through the data, returning a list of all reactive values in the hash, array, etc..



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/volt/reactive/reactive_value.rb', line 214

def self.find_reactives(object)
  found = []
  if object.reactive?
    found << object

    found += find_reactives(object.cur)
  elsif object.is_a?(Array)
    object.each do |item|
      found += find_reactives(item)
    end
  elsif object.is_a?(Hash)
    object.each_pair do |key, value|
      found += find_reactives(key)
      found += find_reactives(value)
    end
  end

  return found.flatten
end

.from_hash(hash, skip_if_no_reactives = false) ⇒ Object

Takes a hash and returns a ReactiveValue that depends on any ReactiveValue’s inside of the hash (or children).



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/volt/reactive/reactive_value.rb', line 193

def self.from_hash(hash, skip_if_no_reactives=false)
  reactives = find_reactives(hash)

  if skip_if_no_reactives && reactives.size == 0
    # There weren't any reactives, we can just use the hash
    return hash
  else
    # Create a new reactive value that listens on all of its
    # child reactive values.
    value = ReactiveValue.new(hash)

    reactives.each do |child|
      value.reactive_manager.add_parent!(child)
    end

    return value
  end
end