Sha256: b38ed79ba666e60c10052c498330d85980496366766a4e82200924b47e26a386

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

module Danica
  module VariablesHolder extend ::ActiveSupport::Concern
    include Common

    autoload :VariablesBuilder, 'danica/variables_holder/variables_builder'
    autoload :AliasBuilder,     'danica/variables_holder/alias_builder'
    autoload :Calculator,       'danica/variables_holder/calculator'
    autoload :Store,            'danica/variables_holder/store'

    delegate :containers_hash, :containers, :variables,
             :variables_hash, :variables_value_hash,
             :extract_variables, to: :store

    included do
      default_value :variable_holder?,  true

      class << self
        def variables(*names)
          VariablesBuilder.new(names, self).build
        end

        def variable_alias(origin, destiny)
          AliasBuilder.new(origin, destiny, self).build
        end

        def variables_names
          variables_hash.keys
        end

        def variables_hash
          @variables_hash ||= superclass_variables_hash.dup
        end

        def reset_variables
          @superclass_variables_hash = {}
          @variables_hash = nil
        end

        def superclass_variables_hash
          @superclass_variables_hash ||= (superclass.try(:variables_hash) || {})
        end
      end
    end

    def initialize(*args)
      args = args.flatten
      args = args.first if (args.length == 1) && args.first.try(:keys).try(:all?) do |k|
        self.class.variables_names.include?(k)
      end
      self.variables = args
    end

    def variables=(vars)
      vars = vars.as_hash(self.class.variables_names).compact unless vars.is_a? Hash
      vars = vars.dup.change_values!(skip_inner: false) { |v| wrap_value(v) }
      vars.each do |k, v|
        public_send("#{k}=", v)
      end
    end

    def calculate(*args)
      Calculator.new(self, *args).calculate
    end

    private

    def store
      @store ||= Store.new(self.class.variables_hash)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
danica-2.7.1 lib/danica/variables_holder.rb
danica-2.6.4 lib/danica/variables_holder.rb
danica-2.6.3 lib/danica/variables_holder.rb
danica-2.6.2 lib/danica/variables_holder.rb
danica-2.6.1 lib/danica/variables_holder.rb