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.
Attributes inherited from Base
#children, #join_options, #name, #options, #parent
Instance Method Summary collapse
-
#child_hash ⇒ Hash{Symbol => Chewy::Fields::Base}
Children indexed by name as a hash.
-
#compose(object, crutches = nil, fields: []) ⇒ Hash
Converts passed object to JSON-ready hash.
- #compose_id(object) ⇒ Object
-
#initialize(name, **options) ⇒ Root
constructor
A new instance of Root.
- #mappings_hash ⇒ Object
- #update_options!(**options) ⇒ Object
Methods inherited from Base
#multi_field?, #object_field?, #value
Constructor Details
#initialize(name, **options) ⇒ Root
Returns a new instance of Root.
6 7 8 9 10 11 |
# File 'lib/chewy/fields/root.rb', line 6 def initialize(name, **) super(name, **) @value ||= -> { self } @dynamic_templates = [] 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.
4 5 6 |
# File 'lib/chewy/fields/root.rb', line 4 def id @id end |
Instance Method Details
#child_hash ⇒ Hash{Symbol => Chewy::Fields::Base}
Children indexed by name as a hash.
87 88 89 |
# File 'lib/chewy/fields/root.rb', line 87 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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chewy/fields/root.rb', line 64 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
51 52 53 54 55 |
# File 'lib/chewy/fields/root.rb', line 51 def compose_id(object) return unless id id.arity.zero? ? object.instance_exec(&id) : id.call(object) end |
#mappings_hash ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/chewy/fields/root.rb', line 18 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) ⇒ Object
13 14 15 16 |
# File 'lib/chewy/fields/root.rb', line 13 def (**) @id = .fetch(:id, .fetch(:_id, @id)) @options.merge!(.except(:id, :_id, :type)) end |