lib/esse/index_mapping.rb in esse-0.2.0 vs lib/esse/index_mapping.rb in esse-0.2.2
- old
+ new
@@ -2,14 +2,15 @@
module Esse
class IndexMapping
FILENAMES = %w[mapping mappings].freeze
- def initialize(body: {}, paths: [], filenames: FILENAMES)
+ 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
@@ -17,10 +18,17 @@
from_template || @mappings
end
def body
- to_h
+ 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