Class: Chewy::Fields::Root
Instance Attribute Summary collapse
-
#dynamic_templates ⇒ Object
readonly
Returns the value of attribute dynamic_templates.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_id ⇒ Object
readonly
Returns the value of attribute parent_id.
Attributes inherited from Base
#children, #name, #options, #value
Instance Method Summary collapse
- #compose_id(object) ⇒ Object
- #compose_parent(object) ⇒ Object
- #dynamic_template(*args) ⇒ Object
-
#initialize(*args) ⇒ Root
constructor
A new instance of Root.
- #mappings_hash ⇒ Object
Methods inherited from Base
#compose, #multi_field?, #object_field?
Constructor Details
#initialize(*args) ⇒ Root
Returns a new instance of Root.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/chewy/fields/root.rb', line 9 def initialize(*args) super(*args) @id = .delete(:id) || .delete(:_id) @parent = .delete(:parent) || .delete(:_parent) @parent_id = .delete(:parent_id) @value ||= ->(_) { _ } @dynamic_templates = [] .delete(:type) end |
Instance Attribute Details
#dynamic_templates ⇒ Object (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 |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/chewy/fields/root.rb', line 5 def id @id end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/chewy/fields/root.rb', line 6 def parent @parent end |
#parent_id ⇒ Object (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
#compose_id(object) ⇒ Object
60 61 62 63 64 |
# File 'lib/chewy/fields/root.rb', line 60 def compose_id(object) if id id.arity == 0 ? object.instance_exec(&id) : id.call(object) end end |
#compose_parent(object) ⇒ Object
54 55 56 57 58 |
# File 'lib/chewy/fields/root.rb', line 54 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/chewy/fields/root.rb', line 33 def dynamic_template *args = args..deep_symbolize_keys if args.first template_name = :"template_#{dynamic_templates.count.next}" template = {template_name => {mapping: }} 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() end end |
#mappings_hash ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/chewy/fields/root.rb', line 20 def mappings_hash mappings = super mappings[name].delete(:type) 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 |