Module: ActiveGraph::Node::QueryMethods

Included in:
Labels::ClassMethods
Defined in:
lib/active_graph/node/query_methods.rb

Instance Method Summary collapse

Instance Method Details

#count(distinct = nil) ⇒ Integer Also known as: size, length

Returns number of nodes of this class.

Returns:

  • (Integer)

    number of nodes of this class



25
26
27
28
29
# File 'lib/active_graph/node/query_methods.rb', line 25

def count(distinct = nil)
  fail(ActiveGraph::InvalidParameterError, ':count accepts the `:distinct` symbol or nil as a parameter') unless distinct.nil? || distinct == :distinct
  q = distinct.nil? ? 'n' : 'DISTINCT n'
  self.query_as(:n).return("count(#{q}) AS count").first[:count]
end

#empty?Boolean Also known as: blank?

Returns:



34
35
36
# File 'lib/active_graph/node/query_methods.rb', line 34

def empty?
  !self.all.exists?
end

#exists?(node_condition = nil) ⇒ Boolean

Returns:



4
5
6
7
8
9
10
11
12
# File 'lib/active_graph/node/query_methods.rb', line 4

def exists?(node_condition = nil)
  unless [Integer, String, Hash, NilClass].any? { |c| node_condition.is_a?(c) }
    fail(ActiveGraph::InvalidParameterError, ':exists? only accepts ids or conditions')
  end
  query_start = exists_query_start(node_condition)
  start_q = query_start.respond_to?(:query_as) ? query_start.query_as(:n) : query_start
  result = start_q.return('ID(n) AS proof_of_life LIMIT 1').first
  !!result
end

#find_each(options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/active_graph/node/query_methods.rb', line 46

def find_each(options = {})
  self.query_as(:n).return(:n).find_each(:n, primary_key, options) do |batch|
    yield batch[:n]
  end
end

#find_in_batches(options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/active_graph/node/query_methods.rb', line 40

def find_in_batches(options = {})
  self.query_as(:n).return(:n).find_in_batches(:n, primary_key, options) do |batch|
    yield batch.map { |record| record[:n] }
  end
end

#firstObject

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



15
16
17
# File 'lib/active_graph/node/query_methods.rb', line 15

def first
  self.query_as(:n).limit(1).order(n: primary_key).pluck(:n).first
end

#lastObject

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



20
21
22
# File 'lib/active_graph/node/query_methods.rb', line 20

def last
  self.query_as(:n).limit(1).order(n: {primary_key => :desc}).pluck(:n).first
end