Module: Neo4j::ActiveNode::Query::QueryProxyEnumerable
- Includes:
- Enumerable
- Included in:
- QueryProxy
- Defined in:
- lib/neo4j/active_node/query/query_proxy_enumerable.rb
Overview
Methods related to returning nodes and rels from QueryProxy
Instance Method Summary collapse
-
#==(other) ⇒ Object
Does exactly what you would hope.
-
#each(node = true, rel = nil, &block) ⇒ Enumerable
Just like every other
eachbut it allows for optional params to support the versions that also return relationships. -
#each_rel(&block) ⇒ Enumerable
When called at the end of a QueryProxy chain, it will return the resultant relationship objects intead of nodes.
-
#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.
- #fetch_result_cache ⇒ Object
-
#pluck(*args) ⇒ Object
For getting variables which have been defined as part of the association chain.
- #result(node = true, rel = nil) ⇒ Object
- #result_cache?(node = true, rel = nil) ⇒ Boolean
- #result_cache_for(node = true, rel = nil) ⇒ Object
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.
70 71 72 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 70 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`.
12 13 14 |
# File 'lib/neo4j/active_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|
54 55 56 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 54 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|
64 65 66 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 64 def each_with_rel(&block) block_given? ? each(true, true, &block) : to_enum(:each, true, true) end |
#fetch_result_cache ⇒ Object
42 43 44 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 42 def fetch_result_cache @result_cache ||= yield end |
#pluck(*args) ⇒ Object
For getting variables which have been defined as part of the association chain
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 75 def pluck(*args) transformable_attributes = (model ? model.attribute_names : []) + %w(uuid neo_id) arg_list = args.map do |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 27 28 29 30 31 32 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 16 def result(node = true, rel = nil) @result_cache ||= {} return result_cache_for(node, rel) if result_cache?(node, rel) pluck_vars = [] pluck_vars << identity if node pluck_vars << @rel_var if rel result = pluck(*pluck_vars) result.each do |object| object.instance_variable_set('@source_query_proxy', self) object.instance_variable_set('@source_proxy_result_cache', result) end @result_cache[[node, rel]] ||= result end |
#result_cache?(node = true, rel = nil) ⇒ Boolean
34 35 36 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 34 def result_cache?(node = true, rel = nil) !!result_cache_for(node, rel) end |
#result_cache_for(node = true, rel = nil) ⇒ Object
38 39 40 |
# File 'lib/neo4j/active_node/query/query_proxy_enumerable.rb', line 38 def result_cache_for(node = true, rel = nil) (@result_cache || {})[[node, rel]] end |