Sha256: 68de8044edfcf09760d8e870567aa5735a81be22943e2e1bd2e6e8f72e744fd5

Contents?: true

Size: 774 Bytes

Versions: 14

Compression:

Stored size: 774 Bytes

Contents

# frozen_string_literal: true

module Esse
  class DynamicTemplate
    # @param [Array, Hash] value The list of dynamic_templates for mapping
    def initialize(value)
      @hash = normalize(value)
    end

    def merge!(value)
      @hash = HashUtils.deep_merge(@hash, normalize(value))
    end

    def []=(key, value)
      merge!(key => value)
    end

    def to_a
      @hash.map do |name, value|
        { name => value }
      end
    end

    def any?
      @hash.any?
    end

    def dup
      self.class.new(@hash.dup)
    end

    private

    def normalize(value)
      case value
      when Array
        value.map { |v| normalize(v) }.reduce(&:merge)
      when Hash
        HashUtils.deep_transform_keys(value, &:to_sym)
      end || {}
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
esse-0.4.0.rc4 lib/esse/dynamic_template.rb
esse-0.4.0.rc3 lib/esse/dynamic_template.rb
esse-0.4.0.rc2 lib/esse/dynamic_template.rb
esse-0.4.0.rc1 lib/esse/dynamic_template.rb
esse-0.3.5 lib/esse/dynamic_template.rb
esse-0.3.4 lib/esse/dynamic_template.rb
esse-0.3.3 lib/esse/dynamic_template.rb
esse-0.3.2 lib/esse/dynamic_template.rb
esse-0.3.1 lib/esse/dynamic_template.rb
esse-0.3.0 lib/esse/dynamic_template.rb
esse-0.2.6 lib/esse/dynamic_template.rb
esse-0.2.5 lib/esse/dynamic_template.rb
esse-0.2.4 lib/esse/dynamic_template.rb
esse-0.2.3 lib/esse/dynamic_template.rb