class Chewy::Fields::Root

Attributes

dynamic_templates[R]
id[R]
parent[R]
parent_id[R]

Public Class Methods

new(name, **options) click to toggle source
Calls superclass method Chewy::Fields::Base::new
# File lib/chewy/fields/root.rb, line 6
def initialize(name, **options)
  super(name, **options)

  @value ||= -> { self }
  @dynamic_templates = []
end

Public Instance Methods

child_hash() click to toggle source

Children indexed by name as a hash.

@return [Hash{Symbol => Chewy::Fields::Base}]

# File lib/chewy/fields/root.rb, line 95
def child_hash
  @child_hash ||= children.index_by(&:name)
end
compose(object, crutches = nil, fields: []) click to toggle source

Converts passed object to JSON-ready hash. Used for objects import.

@param object [Object] a base object for composition @param crutches [Object] any object that will be passed to every field value proc as a last argument @param fields [Array<Symbol>] a list of fields to compose, every field will be composed if empty @return [Hash] JSON-ready hash with stringified keys

# File lib/chewy/fields/root.rb, line 72
def compose(object, crutches = nil, fields: [])
  result = evaluate([object, crutches])

  if children.present?
    child_fields = if fields.present?
      child_hash.slice(*fields).values
    else
      children
    end

    child_fields.each_with_object({}) do |field, memo|
      memo.merge!(field.compose(result, crutches) || {})
    end.as_json
  elsif fields.present?
    result.as_json(only: fields, root: false)
  else
    result.as_json(root: false)
  end
end
compose_id(object) click to toggle source
# File lib/chewy/fields/root.rb, line 59
def compose_id(object)
  return unless id

  id.arity.zero? ? object.instance_exec(&id) : id.call(object)
end
compose_parent(object) click to toggle source
# File lib/chewy/fields/root.rb, line 53
def compose_parent(object)
  return unless parent_id

  parent_id.arity.zero? ? object.instance_exec(&parent_id) : parent_id.call(object)
end
dynamic_template(*args) click to toggle source
# File lib/chewy/fields/root.rb, line 32
def dynamic_template(*args)
  options = args.extract_options!.deep_symbolize_keys
  if args.first
    template_name = :"template_#{dynamic_templates.count.next}"
    template = {template_name => {mapping: options}}

    template[template_name][:match_mapping_type] = args.second.to_s if args.second.present?

    regexp = args.first.is_a?(Regexp)
    template[template_name][:match_pattern] = 'regexp' if regexp

    match = regexp ? args.first.source : args.first
    path = match.include?(regexp ? '\.' : '.')

    template[template_name][path ? :path_match : :match] = match
    @dynamic_templates.push(template)
  else
    @dynamic_templates.push(options)
  end
end
mappings_hash() click to toggle source
Calls superclass method Chewy::Fields::Base#mappings_hash
# File lib/chewy/fields/root.rb, line 20
def mappings_hash
  mappings = super
  mappings[name].delete(:type)

  if dynamic_templates.present?
    mappings[name][:dynamic_templates] ||= []
    mappings[name][:dynamic_templates].concat dynamic_templates
  end

  mappings[name]
end
update_options!(**options) click to toggle source
# File lib/chewy/fields/root.rb, line 13
def update_options!(**options)
  @id = options.fetch(:id, options.fetch(:_id, @id))
  @parent = options.fetch(:parent, options.fetch(:_parent, @parent))
  @parent_id = options.fetch(:parent_id, @parent_id)
  @options.merge!(options.except(:id, :_id, :parent, :_parent, :parent_id, :type))
end