Sha256: e3689ed55a9d0dcdbba3e7334660eb8a150c61c4e49bf44efb32455a183446be

Contents?: true

Size: 1.13 KB

Versions: 15

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Esse
  class IndexMapping
    FILENAMES = %w[mapping mappings].freeze

    def initialize(body: {}, paths: [], filenames: FILENAMES, globals: nil)
      @paths = Array(paths)
      @filenames = Array(filenames)
      @mappings = body
      @globals = globals || -> { {} }
    end

    # This method will be overwrited when passing a block during the
    # mapping defination
    def to_h
      return @mappings unless @mappings.empty?

      from_template || @mappings
    end

    def body
      global = HashUtils.deep_transform_keys(@globals.call, &:to_sym)
      local = HashUtils.deep_transform_keys(to_h.dup, &:to_sym)
      dynamic_template = DynamicTemplate.new(global[:dynamic_templates])
      dynamic_template.merge!(local.delete(:dynamic_templates))
      if dynamic_template.any?
        global[:dynamic_templates] = dynamic_template.to_a
      end
      HashUtils.deep_merge(global, local)
    end

    def empty?
      body.empty?
    end

    protected

    def from_template
      return if @paths.empty?

      loader = Esse::TemplateLoader.new(@paths)
      loader.read(*@filenames)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

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