Module: Neo4j::ActiveNode::Labels::ClassMethods

Includes:
QueryMethods
Defined in:
lib/neo4j/active_node/labels.rb

Instance Method Summary collapse

Methods included from QueryMethods

#count, #empty?, #exists?, #find_each, #find_in_batches, #first, #last

Instance Method Details

#base_classObject



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/neo4j/active_node/labels.rb', line 128

def base_class
  unless self < Neo4j::ActiveNode
    fail "#{name} doesn't belong in a hierarchy descending from ActiveNode"
  end

  if superclass == Object
    self
  else
    superclass.base_class
  end
end

#delete_allObject

Deletes all nodes and connected relationships from Cypher.



102
103
104
105
# File 'lib/neo4j/active_node/labels.rb', line 102

def delete_all
  self.neo4j_session._query("MATCH (n:`#{mapped_label_name}`) OPTIONAL MATCH (n)-[r]-() DELETE n,r")
  self.neo4j_session._query("MATCH (n:`#{mapped_label_name}`) DELETE n")
end

#destroy_allObject

Returns each node to Ruby and calls ‘destroy`. Be careful, as this can be a very slow operation if you have many nodes. It will generate at least one database query per node in the database, more if callbacks require them.



109
110
111
# File 'lib/neo4j/active_node/labels.rb', line 109

def destroy_all
  all.each(&:destroy)
end

#find(id) ⇒ Object

Returns the object with the specified neo4j id.

Parameters:

  • id (String, Integer)

    of node to find



79
80
81
82
83
84
85
86
87
88
# File 'lib/neo4j/active_node/labels.rb', line 79

def find(id)
  map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object }

  result = find_by_id_or_ids(map_id, id)

  fail RecordNotFound.new(
    "Couldn't find #{name} with '#{id_property_name}'=#{id}",
    name, id_property_name, id) if result.blank?
  result.tap { |r| find_callbacks!(r) }
end

#find_by(values) ⇒ Object

Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself.

Parameters:

  • values

    Hash args of arguments to find



92
93
94
# File 'lib/neo4j/active_node/labels.rb', line 92

def find_by(values)
  all.where(values).limit(1).query_as(:n).pluck(:n).first
end

#find_by!(values) ⇒ Object

Like find_by, except that if no record is found, raises a RecordNotFound error.



97
98
99
# File 'lib/neo4j/active_node/labels.rb', line 97

def find_by!(values)
  find_by(values) || fail(RecordNotFound, "#{self.query_as(:n).where(n: values).limit(1).to_cypher} returned no results")
end

#mapped_labelNeo4j::Label

Returns the label for this class.

Returns:

  • (Neo4j::Label)

    the label for this class



124
125
126
# File 'lib/neo4j/active_node/labels.rb', line 124

def mapped_label
  Neo4j::Label.create(mapped_label_name)
end

#mapped_label_nameSymbol

Returns the label that this class has which corresponds to a Ruby class.

Returns:

  • (Symbol)

    the label that this class has which corresponds to a Ruby class



119
120
121
# File 'lib/neo4j/active_node/labels.rb', line 119

def mapped_label_name
  @mapped_label_name || label_for_model
end

#mapped_label_namesArray{Symbol}

Returns all the labels that this class has.

Returns:

  • (Array{Symbol})

    all the labels that this class has



114
115
116
# File 'lib/neo4j/active_node/labels.rb', line 114

def mapped_label_names
  self.ancestors.find_all { |a| a.respond_to?(:mapped_label_name) }.map { |a| a.mapped_label_name.to_sym }
end