class Chewy::Fields::Base
Attributes
children[R]
name[R]
options[R]
parent[RW]
value[R]
Public Class Methods
new(name, value: nil, **options)
click to toggle source
# File lib/chewy/fields/base.rb, line 7 def initialize(name, value: nil, **options) @name = name.to_sym @options = {} update_options!(**options) @value = value @children = [] end
Public Instance Methods
compose(*objects)
click to toggle source
# File lib/chewy/fields/base.rb, line 40 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()
click to toggle source
# File lib/chewy/fields/base.rb, line 27 def mappings_hash mapping = if children.present? {(multi_field? ? :fields : :properties) => children.map(&:mappings_hash).inject(:merge)} else {} end mapping.reverse_merge!(options) mapping.reverse_merge!(type: (children.present? ? 'object' : Chewy.default_field_type)) {name => mapping} end
multi_field?()
click to toggle source
# File lib/chewy/fields/base.rb, line 19 def multi_field? children.present? && !object_field? end
object_field?()
click to toggle source
# File lib/chewy/fields/base.rb, line 23 def object_field? (children.present? && options[:type].blank?) || %w[object nested].include?(options[:type].to_s) end
update_options!(**options)
click to toggle source
# File lib/chewy/fields/base.rb, line 15 def update_options!(**options) @options = options end
Private Instance Methods
compose_children(value, *parent_objects)
click to toggle source
# File lib/chewy/fields/base.rb, line 92 def compose_children(value, *parent_objects) return unless value children.each_with_object({}) do |field, result| result.merge!(field.compose(value, *parent_objects) || {}) end end
evaluate(objects)
click to toggle source
# File lib/chewy/fields/base.rb, line 66 def evaluate(objects) object = objects.first if value.is_a?(Proc) if value.arity.zero? object.instance_exec(&value) elsif value.arity.negative? value.call(*object) else value.call(*objects.first(value.arity)) end else message = value.is_a?(Symbol) || value.is_a?(String) ? value.to_sym : name if object.is_a?(Hash) if object.key?(message) object[message] else object[message.to_s] end else object.send(message) end end end
geo_point?()
click to toggle source
# File lib/chewy/fields/base.rb, line 58 def geo_point? @options[:type].to_s == 'geo_point' end
ignore_blank?()
click to toggle source
# File lib/chewy/fields/base.rb, line 62 def ignore_blank? @options.fetch(:ignore_blank) { geo_point? } end