Class: Chewy::Fields::Base

Inherits:
Object show all
Defined in:
lib/chewy/fields/base.rb

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value: nil, **options) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def options
  @options
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/chewy/fields/base.rb', line 4

def value
  @value
end

Instance Method Details

#compose(*objects) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chewy/fields/base.rb', line 46

def compose(*objects)
  result = evaluate(objects)

  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_hashObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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))

  # This is done to support ES2 journaling and will be removed soon
  if mapping[:type] == 'keyword' && Chewy::Runtime.version < '5.0'
    mapping[:type] = 'string'
    mapping[:index] = 'not_analyzed'
  end

  {name => mapping}
end

#multi_field?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/chewy/fields/base.rb', line 19

def multi_field?
  children.present? && !object_field?
end

#object_field?Boolean

Returns:

  • (Boolean)


23
24
25
# 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) ⇒ Object



15
16
17
# File 'lib/chewy/fields/base.rb', line 15

def update_options!(**options)
  @options = options
end