module ActiveGraph::Node::Query::QueryProxyEnumerable
Methods related to returning nodes and rels from QueryProxy
Public Instance Methods
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.
# File lib/active_graph/node/query/query_proxy_enumerable.rb 64 def ==(other) 65 self.to_a == other 66 end
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`. @return [Enumerable] An enumerable containing some combination of nodes and rels.
# File lib/active_graph/node/query/query_proxy_enumerable.rb 12 def each(node = true, rel = nil, &block) 13 result(node, rel).each(&block) 14 end
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|
@return [Enumerable] An enumerable containing any number of applicable relationship objects.
# File lib/active_graph/node/query/query_proxy_enumerable.rb 48 def each_rel(&block) 49 block_given? ? each(false, true, &block) : to_enum(:each, false, true) 50 end
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|
# File lib/active_graph/node/query/query_proxy_enumerable.rb 58 def each_with_rel(&block) 59 block_given? ? each(true, true, &block) : to_enum(:each, true, true) 60 end
# File lib/active_graph/node/query/query_proxy_enumerable.rb 36 def fetch_result_cache 37 @result_cache ||= yield 38 end
For getting variables which have been defined as part of the association chain
# File lib/active_graph/node/query/query_proxy_enumerable.rb 69 def pluck(*args) 70 transformable_attributes = (model ? model.attribute_names : []) + %w(uuid neo_id) 71 arg_list = args.map do |arg| 72 arg = ActiveGraph::Node::Query::QueryProxy::Link.converted_key(model, arg) 73 if transformable_attributes.include?(arg.to_s) 74 {identity => arg} 75 else 76 arg 77 end 78 end 79 80 self.query.pluck(*arg_list) 81 end
# File lib/active_graph/node/query/query_proxy_enumerable.rb 16 def result(node = true, rel = nil) 17 return [].freeze if unpersisted_start_object? 18 19 @result_cache ||= {} 20 return result_cache_for(node, rel) if result_cache?(node, rel) 21 22 result = pluck_vars(node, rel) 23 set_instance_caches(result, node, rel) 24 25 @result_cache[[node, rel]] ||= result 26 end
# File lib/active_graph/node/query/query_proxy_enumerable.rb 28 def result_cache?(node = true, rel = nil) 29 !!result_cache_for(node, rel) 30 end
# File lib/active_graph/node/query/query_proxy_enumerable.rb 32 def result_cache_for(node = true, rel = nil) 33 (@result_cache || {})[[node, rel]] 34 end
Protected Instance Methods
# File lib/active_graph/node/query/query_proxy_enumerable.rb 85 def ensure_distinct(node, force = false) 86 @distinct || force ? "DISTINCT(#{node})" : node 87 end
Private Instance Methods
# File lib/active_graph/node/query/query_proxy_enumerable.rb 91 def pluck_vars(node, rel) 92 vars = [] 93 vars << ensure_distinct(identity) if node 94 vars << @rel_var if rel 95 pluck(*vars) 96 end
# File lib/active_graph/node/query/query_proxy_enumerable.rb 98 def set_instance_caches(instance, node, rel) 99 instance.each do |object| 100 object.instance_variable_set('@source_query_proxy', self) 101 object.instance_variable_set('@source_proxy_result_cache', instance) 102 if node && rel && object.last.is_a?(ActiveGraph::Relationship) 103 object.last.instance_variable_set(association.direction == :in ? '@from_node' : '@to_node', object.first) 104 end 105 end 106 end