Sha256: a6171d0928d6b54bee227dff113c35664ace195798bfdf2226763ba4a0faffe5

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Danica
  module VariablesHolder extend ::ActiveSupport::Concern
    require 'danica/variables_holder/variables_builder'

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

        def variables_names
          variables_hash.keys
        end

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

    def variables=(vars)
      vars = vars.as_hash(self.class.variables_names).compact unless vars.is_a? Hash
      vars = vars.change_values(skip_inner: false) { |v| wrap_value(v) }
      @variables_hash = self.class.variables_hash.merge(vars)
    end

    def variables
      variables_hash.values
    end
  
    def variables_hash
      @variables_hash ||= {}.merge(self.class.variables_hash)
    end

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

    private

    def non_valued_variables
      variables.reject(&:valued?)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danica-2.4.2 lib/danica/variables_holder.rb
danica-2.4.1 lib/danica/variables_holder.rb
danica-2.4.0 lib/danica/variables_holder.rb