Sha256: 5595dbab3e8fd63cd7a46bafc0793ac52633e26bd14566bb8955ad3a7cf68d66

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module Declarative
  # Implements the pattern of maintaining a hash of key/values (usually "defaults")
  # that are mutated several times by user and library code (override defaults).
  #
  # The Variables instance then represents the configuration data to be processed by the
  # using library (e.g. Representable or Trailblazer).
  class Variables
    class Proc < ::Proc
    end

    # @return Hash hash where `overrides` is merged onto `defaults` respecting Merge, Append etc.
    def self.merge(defaults, overrides)
      defaults = defaults.merge({}) # todo: use our DeepDup. # TODO: or how could we provide immutability?

      overrides.each do |k, v|
        if v.is_a?(Variables::Proc)
          defaults[k] = v.( defaults[k] )
        else
          defaults[k] = v
        end
      end

      defaults
    end

    def self.Merge(merged_hash)
      Variables::Proc.new do |original|
        (original || {}).merge( merged_hash )
      end
    end

    def self.Append(appended_array)
      Variables::Proc.new do |original|
        (original || []) + appended_array
      end
    end
  end # Variables
end

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/declarative-0.0.20/lib/declarative/variables.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/declarative-0.0.20/lib/declarative/variables.rb
declarative-0.0.20 lib/declarative/variables.rb
declarative-0.0.10 lib/declarative/variables.rb