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



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

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.



110
111
112
# File 'lib/active_graph/node/labels.rb', line 110

def delete_all
  neo4j_query("MATCH (n:`#{mapped_label_name}`) DETACH 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.



116
117
118
# File 'lib/active_graph/node/labels.rb', line 116

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



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

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



100
101
102
# File 'lib/active_graph/node/labels.rb', line 100

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.



105
106
107
# File 'lib/active_graph/node/labels.rb', line 105

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 Also known as: mapped_element

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 Also known as: mapped_element_name

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



126
127
128
# File 'lib/active_graph/node/labels.rb', line 126

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



121
122
123
# File 'lib/active_graph/node/labels.rb', line 121

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