module Neo4j::ActiveNode::Query::QueryProxyMethods
Constants
- FIRST
- LAST
Public Instance Methods
Takes an Array of ActiveNode
models and applies the appropriate WHERE clause So for a ‘Teacher` model inheriting from a `Person` model and an `Article` model if you called .as_models([Teacher, Article]) The where clause would look something like:
- .. code-block
-
cypher
WHERE (node_var:Teacher:Person OR node_var:Article)
# File lib/neo4j/active_node/query/query_proxy_methods.rb 156 def as_models(models) 157 where_clause = models.map do |model| 158 "`#{identity}`:" + model.mapped_label_names.map do |mapped_label_name| 159 "`#{mapped_label_name}`" 160 end.join(':') 161 end.join(' OR ') 162 163 where("(#{where_clause})") 164 end
@return [Integer] number of nodes of this class
# File lib/neo4j/active_node/query/query_proxy_methods.rb 39 def count(distinct = nil, target = nil) 40 fail(Neo4j::InvalidParameterError, ':count accepts `distinct` or nil as a parameter') unless distinct.nil? || distinct == :distinct 41 query_with_target(target) do |var| 42 q = distinct.nil? ? var : "DISTINCT #{var}" 43 limited_query = self.query.clause?(:limit) ? self.query.break.with(var) : self.query.reorder 44 limited_query.pluck("count(#{q}) AS #{var}").first 45 end 46 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 61 def empty?(target = nil) 62 query_with_target(target) { |var| !self.exists?(nil, var) } 63 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 82 def exists?(node_condition = nil, target = nil) 83 unless node_condition.is_a?(Integer) || node_condition.is_a?(Hash) || node_condition.nil? 84 fail(Neo4j::InvalidParameterError, ':exists? only accepts neo_ids') 85 end 86 query_with_target(target) do |var| 87 start_q = exists_query_start(node_condition, var) 88 start_q.query.reorder.return("COUNT(#{var}) AS count").first.count > 0 89 end 90 end
Give ability to call ‘#find` on associations to get a scoped find Doesn’t pass through via ‘method_missing` because Enumerable has a `#find` method
# File lib/neo4j/active_node/query/query_proxy_methods.rb 20 def find(*args) 21 scoping { @model.find(*args) } 22 end
When called, this method returns a single node that satisfies the match specified in the params hash. If no existing node is found to satisfy the match, one is created or associated as expected.
# File lib/neo4j/active_node/query/query_proxy_methods.rb 132 def find_or_create_by(params) 133 fail 'Method invalid when called on Class objects' unless source_object 134 result = self.where(params).first 135 return result unless result.nil? 136 Neo4j::Transaction.run do 137 node = model.find_or_create_by(params) 138 self << node 139 return node 140 end 141 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 24 def first(target = nil) 25 first_and_last(FIRST, target) 26 end
Gives you the first relationship between the last link of a QueryProxy
chain and a given node Shorthand for ‘MATCH (start)--(other_node) WHERE ID(other_node) = #{other_node.neo_id} RETURN r` @param [#neo_id, String, Enumerable] node An object to be sent to `match_to`. See params for that method. @return A relationship (ActiveRel
, CypherRelationship, EmbeddedRelationship) or nil.
# File lib/neo4j/active_node/query/query_proxy_methods.rb 118 def first_rel_to(node) 119 self.match_to(node).limit(1).pluck(rel_var).first 120 end
@param [Neo4j::ActiveNode, Neo4j::Node
, String] other An instance of a Neo4j.rb model, a Neo4j-core node, or a string uuid @param [String, Symbol] target An identifier of a link in the Cypher chain @return [Boolean]
# File lib/neo4j/active_node/query/query_proxy_methods.rb 70 def include?(other, target = nil) 71 query_with_target(target) do |var| 72 where_filter = if other.respond_to?(:neo_id) 73 "ID(#{var}) = {other_node_id}" 74 else 75 "#{var}.#{association_id_key} = {other_node_id}" 76 end 77 node_id = other.respond_to?(:neo_id) ? other.neo_id : other 78 self.where(where_filter).params(other_node_id: node_id).query.reorder.return("count(#{var}) as count").first.count > 0 79 end 80 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 28 def last(target = nil) 29 first_and_last(LAST, target) 30 end
TODO: update this with public API methods if/when they are exposed
# File lib/neo4j/active_node/query/query_proxy_methods.rb 55 def limit_value 56 return unless self.query.clause?(:limit) 57 limit_clause = self.query.send(:clauses).find { |clause| clause.is_a?(Neo4j::Core::QueryClauses::LimitClause) } 58 limit_clause.instance_variable_get(:@arg) 59 end
Shorthand for ‘MATCH (start)--(other_node) WHERE ID(other_node) = #{other_node.neo_id}` The `node` param can be a persisted ActiveNode
instance, any string or integer, or nil. When it’s a node, it’ll use the object’s neo_id, which is fastest. When not nil, it’ll figure out the primary key of that model. When nil, it uses ‘1 = 2` to prevent matching all records, which is the default behavior when nil is passed to `where` in QueryProxy
. @param [#neo_id, String, Enumerable] node A node, a string representing a node’s ID, or an enumerable of nodes or IDs. @return [Neo4j::ActiveNode::Query::QueryProxy] A QueryProxy
object upon which you can build.
# File lib/neo4j/active_node/query/query_proxy_methods.rb 99 def match_to(node) 100 first_node = node.is_a?(Array) ? node.first : node 101 where_arg = if first_node.respond_to?(:neo_id) 102 {neo_id: node.is_a?(Array) ? node.map(&:neo_id) : node} 103 elsif !node.nil? 104 {association_id_key => node.is_a?(Array) ? ids_array(node) : node} 105 else 106 # support for null object pattern 107 '1 = 2' 108 end 109 110 self.where(where_arg) 111 end
A shortcut for attaching a new, optional match to the end of a QueryProxy
chain.
# File lib/neo4j/active_node/query/query_proxy_methods.rb 144 def optional(association, node_var = nil, rel_var = nil) 145 self.send(association, node_var, rel_var, optional: true) 146 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 32 def order_property 33 # This should maybe be based on a setting in the association 34 # rather than a hardcoded `nil` 35 model ? model.id_property_name : nil 36 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 14 def rel 15 rels.first 16 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 8 def rels 9 fail 'Cannot get rels without a relationship variable.' if !@rel_var 10 11 pluck(@rel_var) 12 end
Returns all relationships across a QueryProxy
chain between a given node or array of nodes and the preceeding link. @param [#neo_id, String, Enumerable] node An object to be sent to ‘match_to`. See params for that method. @return An enumerable of relationship objects.
# File lib/neo4j/active_node/query/query_proxy_methods.rb 125 def rels_to(node) 126 self.match_to(node).pluck(rel_var) 127 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 48 def size 49 result_cache? ? result_cache_for.length : count 50 end
Private Instance Methods
@return [String] The primary key of a the current QueryProxy’s model or target class
# File lib/neo4j/active_node/query/query_proxy_methods.rb 183 def association_id_key 184 self.association.nil? ? model.primary_key : self.association.target_class.primary_key 185 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 197 def exists_query_start(condition, target) 198 case condition 199 when Integer 200 self.where("ID(#{target}) = {exists_condition}").params(exists_condition: condition) 201 when Hash 202 self.where(condition.keys.first => condition.values.first) 203 else 204 self 205 end 206 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 168 def first_and_last(func, target) 169 new_query, pluck_proc = if self.query.clause?(:order) 170 [self.query.with(identity), 171 proc { |var| "#{func}(COLLECT(#{var})) as #{var}" }] 172 else 173 [self.order(order_property).limit(1), 174 proc { |var| var }] 175 end 176 query_with_target(target) do |var| 177 final_pluck = pluck_proc.call(var) 178 new_query.pluck(final_pluck) 179 end.first 180 end
@param [Enumerable] node An enumerable of nodes or ids. @return [Array] An array after having ‘id` called on each object
# File lib/neo4j/active_node/query/query_proxy_methods.rb 189 def ids_array(node) 190 node.first.respond_to?(:id) ? node.map(&:id) : node 191 end
# File lib/neo4j/active_node/query/query_proxy_methods.rb 193 def query_with_target(target) 194 yield(target || identity) 195 end