Sha256: 1f0e38e5e3db6dc9c66cd13c021f1708143911ca156d2d141fbf1d686d5c9189

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

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

3 entries across 3 versions & 1 rubygems

Version Path
danica-2.7.7 lib/danica/variables_holder/store.rb
danica-2.7.6 lib/danica/variables_holder/store.rb
danica-2.7.5 lib/danica/variables_holder/store.rb