Module: ActiveGraph::Node::Query::QueryProxyEnumerable

Includes:
Enumerable
Included in:
QueryProxy
Defined in:
lib/active_graph/node/query/query_proxy_enumerable.rb

Overview

Methods related to returning nodes and rels from QueryProxy

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Does exactly what you would hope. Without it, comparing `bobby.lessons == sandy.lessons` would evaluate to false because it would be comparing the QueryProxy objects, not the lessons themselves.



64
65
66
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 64

def ==(other)
  self.to_a == other
end

#each(node = true, rel = nil, &block) ⇒ Enumerable

Just like every other each but it allows for optional params to support the versions that also return relationships. The node and rel params are typically used by those other methods but there's nothing stopping you from using `your_node.each(true, true)` instead of `your_node.each_with_rel`.

Returns:

  • (Enumerable)

    An enumerable containing some combination of nodes and rels.



12
13
14
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 12

def each(node = true, rel = nil, &block)
  result(node, rel).each(&block)
end

#each_rel(&block) ⇒ Enumerable

When called at the end of a QueryProxy chain, it will return the resultant relationship objects intead of nodes. For example, to return the relationship between a given student and their lessons:

.. code-block

ruby

student.lessons.each_rel do |rel|

Returns:

  • (Enumerable)

    An enumerable containing any number of applicable relationship objects.



48
49
50
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 48

def each_rel(&block)
  block_given? ? each(false, true, &block) : to_enum(:each, false, true)
end

#each_with_rel(&block) ⇒ Object

When called at the end of a QueryProxy chain, it will return the nodes and relationships of the last link. For example, to return a lesson and each relationship to a given student:

.. code-block

ruby

student.lessons.each_with_rel do |lesson, rel|


58
59
60
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 58

def each_with_rel(&block)
  block_given? ? each(true, true, &block) : to_enum(:each, true, true)
end

#fetch_result_cacheObject



36
37
38
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 36

def fetch_result_cache
  @result_cache ||= yield
end

#pluck(*args) ⇒ Object

For getting variables which have been defined as part of the association chain



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 69

def pluck(*args)
  transformable_attributes = (model ? model.attribute_names : []) + %w(uuid neo_id)
  arg_list = args.map do |arg|
    arg = ActiveGraph::Node::Query::QueryProxy::Link.converted_key(model, arg)
    if transformable_attributes.include?(arg.to_s)
      {identity => arg}
    else
      arg
    end
  end

  self.query.pluck(*arg_list)
end

#result(node = true, rel = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 16

def result(node = true, rel = nil)
  return [].freeze if unpersisted_start_object?

  @result_cache ||= {}
  return result_cache_for(node, rel) if result_cache?(node, rel)

  result = pluck_vars(node, rel)
  set_instance_caches(result, node, rel)

  @result_cache[[node, rel]] ||= result
end

#result_cache?(node = true, rel = nil) ⇒ Boolean

Returns:



28
29
30
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 28

def result_cache?(node = true, rel = nil)
  !!result_cache_for(node, rel)
end

#result_cache_for(node = true, rel = nil) ⇒ Object



32
33
34
# File 'lib/active_graph/node/query/query_proxy_enumerable.rb', line 32

def result_cache_for(node = true, rel = nil)
  (@result_cache || {})[[node, rel]]
end