Class: Chewy::Fields::Base
- Inherits:
-
Object
- Object
- Chewy::Fields::Base
- Defined in:
- lib/chewy/fields/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#join_options ⇒ Object
readonly
Returns the value of attribute join_options.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
used by Chewy::Index::Mapping to expand nested fields.
Instance Method Summary collapse
- #compose(*objects) ⇒ Object
-
#initialize(name, value: nil, **options) ⇒ Base
constructor
A new instance of Base.
- #mappings_hash ⇒ Object
- #multi_field? ⇒ Boolean
- #object_field? ⇒ Boolean
- #update_options!(**options) ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(name, value: nil, **options) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 13 14 |
# File 'lib/chewy/fields/base.rb', line 7 def initialize(name, value: nil, **) @name = name.to_sym @options = {} (**) @value = value @children = [] @allowed_relations = find_allowed_relations([:relations]) # for join fields end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
4 5 6 |
# File 'lib/chewy/fields/base.rb', line 4 def children @children end |
#join_options ⇒ Object (readonly)
Returns the value of attribute join_options.
4 5 6 |
# File 'lib/chewy/fields/base.rb', line 4 def @join_options end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/chewy/fields/base.rb', line 4 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/chewy/fields/base.rb', line 4 def @options end |
#parent ⇒ Object
used by Chewy::Index::Mapping to expand nested fields
5 6 7 |
# File 'lib/chewy/fields/base.rb', line 5 def parent @parent end |
Instance Method Details
#compose(*objects) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chewy/fields/base.rb', line 42 def compose(*objects) result = evaluate(objects) return {} if result.blank? && ignore_blank? if children.present? && !multi_field? result = if result.respond_to?(:to_ary) result.to_ary.map { |item| compose_children(item, *objects) } else compose_children(result, *objects) end end {name => result} end |
#mappings_hash ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/chewy/fields/base.rb', line 29 def mappings_hash mapping = if children.present? {(multi_field? ? :fields : :properties) => children.map(&:mappings_hash).inject(:merge)} else {} end mapping.reverse_merge!() mapping.reverse_merge!(type: (children.present? ? 'object' : Chewy.default_field_type)) {name => mapping} end |
#multi_field? ⇒ Boolean
21 22 23 |
# File 'lib/chewy/fields/base.rb', line 21 def multi_field? children.present? && !object_field? end |
#object_field? ⇒ Boolean
25 26 27 |
# File 'lib/chewy/fields/base.rb', line 25 def object_field? (children.present? && [:type].blank?) || %w[object nested].include?([:type].to_s) end |
#update_options!(**options) ⇒ Object
16 17 18 19 |
# File 'lib/chewy/fields/base.rb', line 16 def (**) @join_options = .delete(:join) || {} @options = end |
#value ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/chewy/fields/base.rb', line 58 def value if join_field? join_type = [:type] join_id = [:id] # memoize @value ||= proc do |object| validate_join_type!(value_by_name_proc(join_type).call(object)) # If it's a join field and it has join_id, the value is compound and contains # both name (type) and id of the parent object if value_by_name_proc(join_id).call(object).present? { name: value_by_name_proc(join_type).call(object), # parent type parent: value_by_name_proc(join_id).call(object) # parent id } else value_by_name_proc(join_type).call(object) end end else @value end end |