Class: Elasticsearch::Model::Extensions::MappingNode

Inherits:
ShortestPath::Node show all
Defined in:
lib/elasticsearch/model/extensions/mapping_node.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ShortestPath::Node

#breadth_first_search, #each_with_name, #edge_class, #eql?, #hash

Constructor Details

#initialize(klass:, name:, mapping:, through_class: nil) ⇒ MappingNode

Returns a new instance of MappingNode.



13
14
15
16
17
18
# File 'lib/elasticsearch/model/extensions/mapping_node.rb', line 13

def initialize(klass:, name:, mapping:, through_class:nil)
  @klass = klass
  @name = name
  @mapping = mapping
  @through_class = through_class
end

Class Method Details

.from_class(klass) ⇒ Object



7
8
9
10
11
# File 'lib/elasticsearch/model/extensions/mapping_node.rb', line 7

def self.from_class(klass)
  name = klass.document_type.intern

  new(klass: klass, name: name, mapping: klass.mapping.to_hash[name])
end

Instance Method Details

#each(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/elasticsearch/model/extensions/mapping_node.rb', line 36

def each(&block)
  associations = @klass.reflect_on_all_associations

  props = @mapping[:properties]
  fields = props.keys

  edges = fields.map { |f|
    a = associations.find { |a| a.name == f }

    if a && a.options[:polymorphic] != true
      through_class = if a.options[:through]
                        a.options[:through].to_s.classify.constantize
                      end

      dest = MappingNode.new(klass: a.class_name.constantize, name: f.to_s.pluralize.intern, mapping: props[f], through_class: through_class)

      edge_class.new(name: f, destination: dest)
    end
  }.reject(&:nil?)

  if block.nil?
    edges
  else
    edges.each(&block)
  end
end

#klassObject



28
29
30
# File 'lib/elasticsearch/model/extensions/mapping_node.rb', line 28

def klass
  @klass
end

#nameObject



20
21
22
# File 'lib/elasticsearch/model/extensions/mapping_node.rb', line 20

def name
  @name
end

#relates_to_class?(klass) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/elasticsearch/model/extensions/mapping_node.rb', line 24

def relates_to_class?(klass)
  @klass == klass || @through_class == klass
end

#through_classObject



32
33
34
# File 'lib/elasticsearch/model/extensions/mapping_node.rb', line 32

def through_class
  @through_class
end