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



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/neo4j/active_node/labels.rb', line 147

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.



122
123
124
# File 'lib/neo4j/active_node/labels.rb', line 122

def delete_all
  neo4j_query("MATCH (n:`#{mapped_label_name}`) OPTIONAL MATCH (n)-[r]-() DELETE n,r")
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.



128
129
130
# File 'lib/neo4j/active_node/labels.rb', line 128

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



99
100
101
102
103
104
105
106
107
108
# File 'lib/neo4j/active_node/labels.rb', line 99

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.inspect}",
    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



112
113
114
# File 'lib/neo4j/active_node/labels.rb', line 112

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.



117
118
119
# File 'lib/neo4j/active_node/labels.rb', line 117

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

#mapped_labelNeo4j::Label

Returns the label for this class.

Returns:

  • (Neo4j::Label)

    the label for this class



143
144
145
# File 'lib/neo4j/active_node/labels.rb', line 143

def mapped_label
  Neo4j::Core::Label.new(mapped_label_name, neo4j_session)
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



138
139
140
# File 'lib/neo4j/active_node/labels.rb', line 138

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



133
134
135
# File 'lib/neo4j/active_node/labels.rb', line 133

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