module Neo4j::ActiveNode::QueryMethods
Public Instance Methods
count(distinct = nil)
click to toggle source
@return [Integer] number of nodes of this class
# File lib/neo4j/active_node/query_methods.rb 24 def count(distinct = nil) 25 fail(Neo4j::InvalidParameterError, ':count accepts `distinct` or nil as a parameter') unless distinct.nil? || distinct == :distinct 26 q = distinct.nil? ? 'n' : 'DISTINCT n' 27 self.query_as(:n).return("count(#{q}) AS count").first.count 28 end
empty?()
click to toggle source
# File lib/neo4j/active_node/query_methods.rb 33 def empty? 34 !self.all.exists? 35 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 node_condition.is_a?(Integer) || node_condition.is_a?(Hash) || node_condition.nil? 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 start_q.return('COUNT(n) AS count').first.count > 0 11 end
find_each(options = {}) { |n| ... }
click to toggle source
# File lib/neo4j/active_node/query_methods.rb 45 def find_each(options = {}) 46 self.query_as(:n).return(:n).find_each(:n, primary_key, options) do |batch| 47 yield batch.n 48 end 49 end
find_in_batches(options = {}) { |map(&:n)| ... }
click to toggle source
# File lib/neo4j/active_node/query_methods.rb 39 def find_in_batches(options = {}) 40 self.query_as(:n).return(:n).find_in_batches(:n, primary_key, options) do |batch| 41 yield batch.map(&:n) 42 end 43 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 14 def first 15 self.query_as(:n).limit(1).order(n: primary_key).pluck(:n).first 16 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 19 def last 20 self.query_as(:n).limit(1).order(n: {primary_key => :desc}).pluck(:n).first 21 end
Private Instance Methods
exists_query_start(node_condition)
click to toggle source
# File lib/neo4j/active_node/query_methods.rb 53 def exists_query_start(node_condition) 54 case node_condition 55 when Integer 56 self.query_as(:n).where('ID(n)' => node_condition) 57 when Hash 58 self.where(node_condition.keys.first => node_condition.values.first) 59 else 60 self.query_as(:n) 61 end 62 end