Module: ActiveGraph::Node::Labels::ClassMethods

Includes:
QueryMethods
Defined in:
lib/active_graph/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



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/active_graph/node/labels.rb', line 137

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

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

#delete_allObject

Deletes all nodes and connected relationships from Cypher.



112
113
114
# File 'lib/active_graph/node/labels.rb', line 112

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.



118
119
120
# File 'lib/active_graph/node/labels.rb', line 118

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



89
90
91
92
93
94
95
96
97
98
# File 'lib/active_graph/node/labels.rb', line 89

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



102
103
104
# File 'lib/active_graph/node/labels.rb', line 102

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.



107
108
109
# File 'lib/active_graph/node/labels.rb', line 107

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_labelActiveGraph::Label

Returns the label for this class.

Returns:

  • (ActiveGraph::Label)

    the label for this class



133
134
135
# File 'lib/active_graph/node/labels.rb', line 133

def mapped_label
  ActiveGraph::Core::Label.new(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



128
129
130
# File 'lib/active_graph/node/labels.rb', line 128

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



123
124
125
# File 'lib/active_graph/node/labels.rb', line 123

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