Class: Elasticsearch::Model::Extensions::MappingReflection::MappingReflector

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/model/extensions/mapping_reflection/mapping_reflector.rb

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ MappingReflector

Returns a new instance of MappingReflector.

Parameters:

  • base (Class)

    A class extending ActiveRecord::Base



7
8
9
# File 'lib/elasticsearch/model/extensions/mapping_reflection/mapping_reflector.rb', line 7

def initialize(base)
  @base = base
end

Instance Method Details

#baseObject



11
12
13
# File 'lib/elasticsearch/model/extensions/mapping_reflection/mapping_reflector.rb', line 11

def base
  @base
end

#has_document_field_named?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/elasticsearch/model/extensions/mapping_reflection/mapping_reflector.rb', line 70

def has_document_field_named?(field_name)
  !! document_field_named(field_name)
end

#nested_object_fields_for(path, root_properties: nil) ⇒ Object

Parameters:

  • path (Array<Symbol>)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/elasticsearch/model/extensions/mapping_reflection/mapping_reflector.rb', line 75

def nested_object_fields_for(path, root_properties: nil)
  root_properties ||= default_root_properties

  keys = root_properties.keys

  suffix, *postfix = path

  return root_properties.keys if suffix.nil?

  keys.each do |key|
    if key == suffix
      result = nested_object_fields_for(postfix, root_properties: root_properties[key][:properties])
      return result if result
    end
  end
end

#path_in_mapping_to(nested_object_name, root_properties: nil) ⇒ Array<Symbol>

Parameters:

  • nested_object_name (Symbol)

Returns:

  • (Array<Symbol>)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/elasticsearch/model/extensions/mapping_reflection/mapping_reflector.rb', line 49

def path_in_mapping_to(nested_object_name, root_properties: nil)
  root_properties ||= default_root_properties

  keys = root_properties.keys

  keys.each do |key|
    if key == nested_object_name
      return [key]
    end

    next if root_properties[key][:type] != 'object'

    suffix = path_in_mapping_to(nested_object_name, root_properties: root_properties[key][:properties])

    if suffix.include? nested_object_name
      return [key] + suffix
    end
  end
  []
end

#path_in_mapping_to_class(destination_class, current_properties: nil, current_class: nil, visited_classes: nil) ⇒ Object

Parameters:

  • destination_class (Class)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elasticsearch/model/extensions/mapping_reflection/mapping_reflector.rb', line 16

def path_in_mapping_to_class(destination_class, current_properties: nil, current_class: nil, visited_classes: nil)
  current_properties ||= default_root_properties
  visited_classes ||= []
  current_class ||= self

  # Recurse only on associations
  current_properties.keys.each do |key|
    association_found = current_class.reflect_on_all_associations.find { |a| a.name == key }

    next unless association_found
    next if visited_classes.include? association_found.klass

    if association_found.klass == destination_class
      return [key]
    else
      suffix_found = path_in_mapping_to_class(
        destination_class,
        current_properties: current_properties[key][:properties],
        current_class: association_found.klass,
        visited_classes: visited_classes.dup.append(association_found.klass)
      )

      if suffix_found
        return [key] + suffix_found
      end
    end
  end

  nil
end