Module: Neo4j::ActiveNode::QueryMethods
- Included in:
- Labels::ClassMethods
- Defined in:
- lib/neo4j/active_node/query_methods.rb
Instance Method Summary collapse
-
#count(distinct = nil) ⇒ Integer
(also: #size, #length)
Number of nodes of this class.
- #empty? ⇒ Boolean (also: #blank?)
- #exists?(node_condition = nil) ⇒ Boolean
- #find_each(options = {}) ⇒ Object
- #find_in_batches(options = {}) ⇒ Object
-
#first ⇒ Object
Returns the first node of this class, sorted by ID.
-
#last ⇒ Object
Returns the last node of this class, sorted by ID.
Instance Method Details
#count(distinct = nil) ⇒ Integer Also known as: size, length
Returns number of nodes of this class.
24 25 26 27 28 |
# File 'lib/neo4j/active_node/query_methods.rb', line 24 def count(distinct = nil) fail(Neo4j::InvalidParameterError, ':count accepts `distinct` 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?
33 34 35 |
# File 'lib/neo4j/active_node/query_methods.rb', line 33 def empty? !self.all.exists? end |
#exists?(node_condition = nil) ⇒ Boolean
4 5 6 7 8 9 10 11 |
# File 'lib/neo4j/active_node/query_methods.rb', line 4 def exists?(node_condition = nil) unless node_condition.is_a?(Integer) || node_condition.is_a?(Hash) || node_condition.nil? fail(Neo4j::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 start_q.return('COUNT(n) AS count').first.count > 0 end |
#find_each(options = {}) ⇒ Object
45 46 47 48 49 |
# File 'lib/neo4j/active_node/query_methods.rb', line 45 def find_each( = {}) self.query_as(:n).return(:n).find_each(:n, primary_key, ) do |batch| yield batch.n end end |
#find_in_batches(options = {}) ⇒ Object
39 40 41 42 43 |
# File 'lib/neo4j/active_node/query_methods.rb', line 39 def find_in_batches( = {}) self.query_as(:n).return(:n).find_in_batches(:n, primary_key, ) do |batch| yield batch.map(&:n) end end |
#first ⇒ Object
Returns the first node of this class, sorted by ID. Note that this may not be the first node created since Neo4j recycles IDs.
14 15 16 |
# File 'lib/neo4j/active_node/query_methods.rb', line 14 def first self.query_as(:n).limit(1).order(n: primary_key).pluck(:n).first end |
#last ⇒ Object
Returns the last node of this class, sorted by ID. Note that this may not be the first node created since Neo4j recycles IDs.
19 20 21 |
# File 'lib/neo4j/active_node/query_methods.rb', line 19 def last self.query_as(:n).limit(1).order(n: {primary_key => :desc}).pluck(:n).first end |