module Neo4j::ActiveNode::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/neo4j/active_node/query/query_proxy_enumerable.rb 70 def ==(other) 71 self.to_a == other 72 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/neo4j/active_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/neo4j/active_node/query/query_proxy_enumerable.rb 54 def each_rel(&block) 55 block_given? ? each(false, true, &block) : to_enum(:each, false, true) 56 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/neo4j/active_node/query/query_proxy_enumerable.rb 64 def each_with_rel(&block) 65 block_given? ? each(true, true, &block) : to_enum(:each, true, true) 66 end
# File lib/neo4j/active_node/query/query_proxy_enumerable.rb 42 def fetch_result_cache 43 @result_cache ||= yield 44 end
For getting variables which have been defined as part of the association chain
# File lib/neo4j/active_node/query/query_proxy_enumerable.rb 75 def pluck(*args) 76 transformable_attributes = (model ? model.attribute_names : []) + %w(uuid neo_id) 77 arg_list = args.map do |arg| 78 if transformable_attributes.include?(arg.to_s) 79 {identity => arg} 80 else 81 arg 82 end 83 end 84 85 self.query.pluck(*arg_list) 86 end
# File lib/neo4j/active_node/query/query_proxy_enumerable.rb 16 def result(node = true, rel = nil) 17 @result_cache ||= {} 18 return result_cache_for(node, rel) if result_cache?(node, rel) 19 20 pluck_vars = [] 21 pluck_vars << identity if node 22 pluck_vars << @rel_var if rel 23 24 result = pluck(*pluck_vars) 25 26 result.each do |object| 27 object.instance_variable_set('@source_query_proxy', self) 28 object.instance_variable_set('@source_proxy_result_cache', result) 29 end 30 31 @result_cache[[node, rel]] ||= result 32 end
# File lib/neo4j/active_node/query/query_proxy_enumerable.rb 34 def result_cache?(node = true, rel = nil) 35 !!result_cache_for(node, rel) 36 end
# File lib/neo4j/active_node/query/query_proxy_enumerable.rb 38 def result_cache_for(node = true, rel = nil) 39 (@result_cache || {})[[node, rel]] 40 end