Class: Chewy::Fields::Root

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

Instance Attribute Summary collapse

Attributes inherited from Base

#name, #options, #value

Instance Method Summary collapse

Methods inherited from Base

#compose, #nested

Constructor Details

#initialize(name, options = {}) ⇒ Root

Returns a new instance of Root.



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

def initialize(name, options = {})
  @parent = options.delete(:parent) || options.delete(:_parent)
  @parent_id = options.delete(:parent_id)
  options.reverse_merge!(value: ->(_){_})
  super(name, options)
  options.delete(:type)
  @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

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



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

def parent_id
  @parent_id
end

Instance Method Details

#compose_parent(object) ⇒ Object



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

def compose_parent(object)
  if parent_id
    parent_id.arity == 0 ? object.instance_exec(&parent_id) : parent_id.call(object)
  end
end

#dynamic_template(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chewy/fields/root.rb', line 41

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



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chewy/fields/root.rb', line 29

def mappings_hash
  mappings = super

  if dynamic_templates.any?
    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

#multi_field?Boolean

Returns:

  • (Boolean)


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

def multi_field?
  false
end

#object_field?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/chewy/fields/root.rb', line 21

def object_field?
  true
end

#root_field?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/chewy/fields/root.rb', line 25

def root_field?
  true
end