Class: Chewy::Fields::Root

Inherits:
Base show all
Defined in:
lib/chewy/fields/root.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#children, #name, #options, #value

Instance Method Summary collapse

Methods inherited from Base

#multi_field?, #object_field?

Constructor Details

#initializeRoot

Returns a new instance of Root.



9
10
11
12
13
14
# File 'lib/chewy/fields/root.rb', line 9

def initialize(*)
  super

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

Instance Attribute Details

#dynamic_templatesObject (readonly)

Returns the value of attribute dynamic_templates.



4
5
6
# File 'lib/chewy/fields/root.rb', line 4

def dynamic_templates
  @dynamic_templates
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/chewy/fields/root.rb', line 5

def id
  @id
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/chewy/fields/root.rb', line 6

def parent
  @parent
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



7
8
9
# File 'lib/chewy/fields/root.rb', line 7

def parent_id
  @parent_id
end

Instance Method Details

#child_hashHash{Symbol => Chewy::Fields::Base}

Children indexed by name as a hash.



97
98
99
# File 'lib/chewy/fields/root.rb', line 97

def child_hash
  @child_hash ||= children.index_by(&:name)
end

#compose(object, crutches = nil, fields: []) ⇒ Hash

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

Parameters:

  • object (Object)

    a base object for composition

  • crutches (Object) (defaults to: nil)

    any object that will be passed to every field value proc as a last argument

  • fields (Array<Symbol>) (defaults to: [])

    a list of fields to compose, every field will be composed if empty

Returns:

  • (Hash)

    JSON-ready hash with stringified keys



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chewy/fields/root.rb', line 74

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) ⇒ Object



62
63
64
65
# File 'lib/chewy/fields/root.rb', line 62

def compose_id(object)
  return unless id
  id.arity.zero? ? object.instance_exec(&id) : id.call(object)
end

#compose_parent(object) ⇒ Object



57
58
59
60
# File 'lib/chewy/fields/root.rb', line 57

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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chewy/fields/root.rb', line 36

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_hashObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chewy/fields/root.rb', line 23

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][:_parent] = parent.is_a?(Hash) ? parent : {type: parent} if parent
  mappings
end

#update_options!(**options) ⇒ Object



16
17
18
19
20
21
# File 'lib/chewy/fields/root.rb', line 16

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