module Neo4j::ActiveNode::Labels::ClassMethods

Public Instance Methods

base_class() click to toggle source
    # File lib/neo4j/active_node/labels.rb
147 def base_class
148   unless self < Neo4j::ActiveNode
149     fail "#{name} doesn't belong in a hierarchy descending from ActiveNode"
150   end
151 
152   if superclass == Object
153     self
154   else
155     superclass.base_class
156   end
157 end
delete_all() click to toggle source

Deletes all nodes and connected relationships from Cypher.

    # File lib/neo4j/active_node/labels.rb
122 def delete_all
123   neo4j_query("MATCH (n:`#{mapped_label_name}`) OPTIONAL MATCH (n)-[r]-() DELETE n,r")
124 end
destroy_all() click to toggle source

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.

    # File lib/neo4j/active_node/labels.rb
128 def destroy_all
129   all.each(&:destroy)
130 end
find(id) click to toggle source

Returns the object with the specified neo4j id. @param [String,Integer] id of node to find

    # File lib/neo4j/active_node/labels.rb
 99 def find(id)
100   map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object }
101 
102   result = find_by_id_or_ids(map_id, id)
103 
104   fail RecordNotFound.new(
105     "Couldn't find #{name} with '#{id_property_name}'=#{id.inspect}",
106     name, id_property_name, id) if result.blank?
107   result.tap { |r| find_callbacks!(r) }
108 end
find_by(values) click to toggle source

Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself. @param values Hash args of arguments to find

    # File lib/neo4j/active_node/labels.rb
112 def find_by(values)
113   all.where(values).limit(1).query_as(:n).pluck(:n).first
114 end
find_by!(values) click to toggle source

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

    # File lib/neo4j/active_node/labels.rb
117 def find_by!(values)
118   find_by(values) || fail(RecordNotFound.new("#{self.query_as(:n).where(n: values).limit(1).to_cypher} returned no results", name))
119 end
mapped_label() click to toggle source

@return [Neo4j::Label] the label for this class

    # File lib/neo4j/active_node/labels.rb
143 def mapped_label
144   Neo4j::Core::Label.new(mapped_label_name, neo4j_session)
145 end
mapped_label_name() click to toggle source

@return [Symbol] the label that this class has which corresponds to a Ruby class

    # File lib/neo4j/active_node/labels.rb
138 def mapped_label_name
139   @mapped_label_name || label_for_model
140 end
mapped_label_names() click to toggle source

@return [Array{Symbol}] all the labels that this class has

    # File lib/neo4j/active_node/labels.rb
133 def mapped_label_names
134   self.ancestors.find_all { |a| a.respond_to?(:mapped_label_name) }.map { |a| a.mapped_label_name.to_sym }
135 end

Protected Instance Methods

mapped_label_name=(name) click to toggle source
    # File lib/neo4j/active_node/labels.rb
165 def mapped_label_name=(name)
166   @mapped_label_name = name.to_sym
167 end
mapped_labels() click to toggle source
    # File lib/neo4j/active_node/labels.rb
161 def mapped_labels
162   mapped_label_names.map { |label_name| Neo4j::Label.create(label_name) }
163 end
set_mapped_label_name(name) click to toggle source

rubocop:disable Naming/AccessorMethodName

    # File lib/neo4j/active_node/labels.rb
170 def set_mapped_label_name(name)
171   ActiveSupport::Deprecation.warn 'set_mapped_label_name is deprecated, use self.mapped_label_name= instead.', caller
172 
173   self.mapped_label_name = name
174 end

Private Instance Methods

decorated_label_name() click to toggle source
    # File lib/neo4j/active_node/labels.rb
202 def decorated_label_name
203   name =  case Neo4j::Config[:module_handling]
204           when :demodulize
205             self.name.demodulize
206           when Proc
207             Neo4j::Config[:module_handling].call self.name
208           else
209             self.name
210           end
211 
212   name.to_sym
213 end
find_by_id_or_ids(map_id, id) click to toggle source

rubocop:enable Naming/AccessorMethodName

    # File lib/neo4j/active_node/labels.rb
179 def find_by_id_or_ids(map_id, id)
180   if id.is_a?(Array)
181     find_by_ids(id.map(&map_id))
182   else
183     find_by_id(map_id.call(id))
184   end
185 end
find_callbacks!(result) click to toggle source
    # File lib/neo4j/active_node/labels.rb
187 def find_callbacks!(result)
188   case result
189   when Neo4j::ActiveNode
190     result.run_callbacks(:find)
191   when Array
192     result.each { |r| find_callbacks!(r) }
193   else
194     result
195   end
196 end
label_for_model() click to toggle source
    # File lib/neo4j/active_node/labels.rb
198 def label_for_model
199   (self.name.nil? ? object_id.to_s.to_sym : decorated_label_name)
200 end