Sha256: a16a85a8f7ca688bf9317929acb6147926994e73303307cb7315ab561ac6f92b

Contents?: true

Size: 1009 Bytes

Versions: 1

Compression:

Stored size: 1009 Bytes

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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danica-2.4.3 lib/danica/variables_holder.rb