module Neo4j::ActiveNode::QueryMethods

Public Instance Methods

blank?()
Alias for: empty?
count(distinct = nil) click to toggle source

@return [Integer] number of nodes of this class

   # File lib/neo4j/active_node/query_methods.rb
25 def count(distinct = nil)
26   fail(Neo4j::InvalidParameterError, ':count accepts the `:distinct` symbol or nil as a parameter') unless distinct.nil? || distinct == :distinct
27   q = distinct.nil? ? 'n' : 'DISTINCT n'
28   self.query_as(:n).return("count(#{q}) AS count").first.count
29 end
Also aliased as: size, length
empty?() click to toggle source
   # File lib/neo4j/active_node/query_methods.rb
34 def empty?
35   !self.all.exists?
36 end
Also aliased as: blank?
exists?(node_condition = nil) click to toggle source
   # File lib/neo4j/active_node/query_methods.rb
 4 def exists?(node_condition = nil)
 5   unless [Integer, String, Hash, NilClass].any? { |c| node_condition.is_a?(c) }
 6     fail(Neo4j::InvalidParameterError, ':exists? only accepts ids or conditions')
 7   end
 8   query_start = exists_query_start(node_condition)
 9   start_q = query_start.respond_to?(:query_as) ? query_start.query_as(:n) : query_start
10   result = start_q.return('ID(n) AS proof_of_life LIMIT 1').first
11   !!result
12 end
find_each(options = {}) { |n| ... } click to toggle source
   # File lib/neo4j/active_node/query_methods.rb
46 def find_each(options = {})
47   self.query_as(:n).return(:n).find_each(:n, primary_key, options) do |batch|
48     yield batch.n
49   end
50 end
find_in_batches(options = {}) { |map(&:n)| ... } click to toggle source
   # File lib/neo4j/active_node/query_methods.rb
40 def find_in_batches(options = {})
41   self.query_as(:n).return(:n).find_in_batches(:n, primary_key, options) do |batch|
42     yield batch.map(&:n)
43   end
44 end
first() click to toggle source

Returns the first node of this class, sorted by ID. Note that this may not be the first node created since Neo4j recycles IDs.

   # File lib/neo4j/active_node/query_methods.rb
15 def first
16   self.query_as(:n).limit(1).order(n: primary_key).pluck(:n).first
17 end
last() click to toggle source

Returns the last node of this class, sorted by ID. Note that this may not be the first node created since Neo4j recycles IDs.

   # File lib/neo4j/active_node/query_methods.rb
20 def last
21   self.query_as(:n).limit(1).order(n: {primary_key => :desc}).pluck(:n).first
22 end
length(distinct = nil)
Alias for: count
size(distinct = nil)
Alias for: count

Private Instance Methods

exists_query_start(node_condition) click to toggle source
   # File lib/neo4j/active_node/query_methods.rb
54 def exists_query_start(node_condition)
55   case node_condition
56   when Integer
57     self.query_as(:n).where('ID(n)' => node_condition)
58   when String
59     self.query_as(:n).where(n: {primary_key => node_condition})
60   when Hash
61     self.where(node_condition.keys.first => node_condition.values.first)
62   else
63     self.query_as(:n)
64   end
65 end