Sha256: a0797b5736552bfd53f92589219406913d0682590b6fdc8f725ff27c6ae7946d

Contents?: true

Size: 1.16 KB

Versions: 21

Compression:

Stored size: 1.16 KB

Contents

class ReactiveGenerator
  # Takes a hash and returns a ReactiveValue that depends on
  # any ReactiveValue's inside of the hash (or children).
  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

  # Recursively loop through the data, returning a list of all
  # reactive values in the hash, array, etc..
  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
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
volt-0.7.23 lib/volt/reactive/reactive_generator.rb
volt-0.7.22 lib/volt/reactive/reactive_generator.rb
volt-0.7.21 lib/volt/reactive/reactive_generator.rb
volt-0.7.20 lib/volt/reactive/reactive_generator.rb
volt-0.7.19 lib/volt/reactive/reactive_generator.rb
volt-0.7.18 lib/volt/reactive/reactive_generator.rb
volt-0.7.17 lib/volt/reactive/reactive_generator.rb
volt-0.7.16 lib/volt/reactive/reactive_generator.rb
volt-0.7.15 lib/volt/reactive/reactive_generator.rb
volt-0.7.14 lib/volt/reactive/reactive_generator.rb
volt-0.7.13 lib/volt/reactive/reactive_generator.rb
volt-0.7.12 lib/volt/reactive/reactive_generator.rb
volt-0.7.10 lib/volt/reactive/reactive_generator.rb
volt-0.7.9 lib/volt/reactive/reactive_generator.rb
volt-0.7.8 lib/volt/reactive/reactive_generator.rb
volt-0.7.7 lib/volt/reactive/reactive_generator.rb
volt-0.7.6 lib/volt/reactive/reactive_generator.rb
volt-0.7.5 lib/volt/reactive/reactive_generator.rb
volt-0.7.4 lib/volt/reactive/reactive_generator.rb
volt-0.7.3 lib/volt/reactive/reactive_generator.rb