Module: Elasticsearch::Model::Extensions::MappingReflection::ClassMethods

Defined in:
lib/elasticsearch/model/extensions/mapping_reflection.rb

Instance Method Summary collapse

Instance Method Details

#document_field_named(field_name) ⇒ Object



65
66
67
68
# File 'lib/elasticsearch/model/extensions/mapping_reflection.rb', line 65

def document_field_named(field_name)
  root_properties ||= mappings.to_hash[:"#{self.document_type}"][:properties]
  root_properties[field_name]
end

#has_document_field_named?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/elasticsearch/model/extensions/mapping_reflection.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.rb', line 75

def nested_object_fields_for(path, root_properties: nil)
  root_properties ||= mappings.to_hash[:"#{self.document_type}"][: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>)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/elasticsearch/model/extensions/mapping_reflection.rb', line 44

def path_in_mapping_to(nested_object_name, root_properties: nil)
  root_properties ||= mappings.to_hash[:"#{self.document_type}"][: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)


11
12
13
14
15
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
# File 'lib/elasticsearch/model/extensions/mapping_reflection.rb', line 11

def path_in_mapping_to_class(destination_class, current_properties: nil, current_class: nil, visited_classes: nil)
  current_properties ||= mappings.to_hash[:"#{self.document_type}"][: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