Sha256: b4e78d3a40c406b07da73d93e501faba0ac98fc3182f183653a4df6554ba358c

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

module Danica
  class VariablesHolder::Store
    attr_reader :default_variables_hash, :variables_names

    def initialize(default_variables_hash)
      @default_variables_hash = default_variables_hash
      @variables_names = default_variables_hash.keys
    end

    def containers_hash
      @containers_hash ||= {}.merge(default_containers_hash)
    end

    def containers
      containers_hash.values
    end

    def variables
      containers.map(&:content)
    end

    def variables_hash
      containers_hash.change_values(&:content)
    end

    def variables_value_hash
      variables.map do |var|
        var.try(:value)
      end.as_hash(variables_names)
    end

    def extract_variables
      inner_containers_hash.merge(named_variables_hash)
    end

    private

    def named_variables_hash
      variable_variables.inject({}) do |hash, (key, container)|
        hash.merge( (container.content.name || key).to_sym => container )
      end
    end

    def variable_variables
      containers_hash.select do |_, container|
        container.variable?
      end
    end

    def inner_containers_hash
      variable_holders.inject({}) do |hash, container|
        hash.merge!(container.content.extract_variables)
      end
    end

    def variable_holders
      variables.select(&:variable_holder?)
    end

    def default_containers_hash
      default_variables_hash.change_values do |value|
        Wrapper::Container.new(value)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
danica-2.7.4 lib/danica/variables_holder/store.rb
danica-2.7.3 lib/danica/variables_holder/store.rb
danica-2.7.2 lib/danica/variables_holder/store.rb
danica-2.7.1 lib/danica/variables_holder/store.rb
danica-2.6.4 lib/danica/variables_holder/store.rb
danica-2.6.3 lib/danica/variables_holder/store.rb
danica-2.6.2 lib/danica/variables_holder/store.rb
danica-2.6.1 lib/danica/variables_holder/store.rb