module Neo4j::ActiveNode::Labels::ClassMethods

Public Instance Methods

base_class() click to toggle source
    # File lib/neo4j/active_node/labels.rb
128 def base_class
129   unless self < Neo4j::ActiveNode
130     fail "#{name} doesn't belong in a hierarchy descending from ActiveNode"
131   end
132 
133   if superclass == Object
134     self
135   else
136     superclass.base_class
137   end
138 end
delete_all() click to toggle source

Deletes all nodes and connected relationships from Cypher.

    # File lib/neo4j/active_node/labels.rb
102 def delete_all
103   self.neo4j_session._query("MATCH (n:`#{mapped_label_name}`) OPTIONAL MATCH (n)-[r]-() DELETE n,r")
104   self.neo4j_session._query("MATCH (n:`#{mapped_label_name}`) DELETE n")
105 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
109 def destroy_all
110   all.each(&:destroy)
111 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
79 def find(id)
80   map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object }
81 
82   result = find_by_id_or_ids(map_id, id)
83 
84   fail RecordNotFound.new(
85     "Couldn't find #{name} with '#{id_property_name}'=#{id}",
86     name, id_property_name, id) if result.blank?
87   result.tap { |r| find_callbacks!(r) }
88 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
92 def find_by(values)
93   all.where(values).limit(1).query_as(:n).pluck(:n).first
94 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
97 def find_by!(values)
98   find_by(values) || fail(RecordNotFound, "#{self.query_as(:n).where(n: values).limit(1).to_cypher} returned no results")
99 end
mapped_label() click to toggle source

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

    # File lib/neo4j/active_node/labels.rb
124 def mapped_label
125   Neo4j::Label.create(mapped_label_name)
126 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
119 def mapped_label_name
120   @mapped_label_name || label_for_model
121 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
114 def mapped_label_names
115   self.ancestors.find_all { |a| a.respond_to?(:mapped_label_name) }.map { |a| a.mapped_label_name.to_sym }
116 end

Protected Instance Methods

mapped_label_name=(name) click to toggle source
    # File lib/neo4j/active_node/labels.rb
146 def mapped_label_name=(name)
147   @mapped_label_name = name.to_sym
148 end
mapped_labels() click to toggle source
    # File lib/neo4j/active_node/labels.rb
142 def mapped_labels
143   mapped_label_names.map { |label_name| Neo4j::Label.create(label_name) }
144 end
set_mapped_label_name(name) click to toggle source

rubocop:disable Style/AccessorMethodName

    # File lib/neo4j/active_node/labels.rb
151 def set_mapped_label_name(name)
152   ActiveSupport::Deprecation.warn 'set_mapped_label_name is deprecated, use self.mapped_label_name= instead.', caller
153 
154   self.mapped_label_name = name
155 end

Private Instance Methods

decorated_label_name() click to toggle source
    # File lib/neo4j/active_node/labels.rb
183 def decorated_label_name
184   name =  case Neo4j::Config[:module_handling]
185           when :demodulize
186             self.name.demodulize
187           when Proc
188             Neo4j::Config[:module_handling].call self.name
189           else
190             self.name
191           end
192 
193   name.to_sym
194 end
find_by_id_or_ids(map_id, id) click to toggle source

rubocop:enable Style/AccessorMethodName

    # File lib/neo4j/active_node/labels.rb
160 def find_by_id_or_ids(map_id, id)
161   if id.is_a?(Array)
162     find_by_ids(id.map(&map_id))
163   else
164     find_by_id(map_id.call(id))
165   end
166 end
find_callbacks!(result) click to toggle source
    # File lib/neo4j/active_node/labels.rb
168 def find_callbacks!(result)
169   case result
170   when Neo4j::ActiveNode
171     result.run_callbacks(:find)
172   when Array
173     result.each { |r| find_callbacks!(r) }
174   else
175     result
176   end
177 end
label_for_model() click to toggle source
    # File lib/neo4j/active_node/labels.rb
179 def label_for_model
180   (self.name.nil? ? object_id.to_s.to_sym : decorated_label_name)
181 end