module ActiveGraph::Node::Labels::ClassMethods
Public Instance Methods
# File lib/active_graph/node/labels.rb 137 def base_class 138 unless self < ActiveGraph::Node 139 fail "#{name} doesn't belong in a hierarchy descending from Node" 140 end 141 142 if superclass == Object 143 self 144 else 145 superclass.base_class 146 end 147 end
Deletes all nodes and connected relationships from Cypher.
# File lib/active_graph/node/labels.rb 112 def delete_all 113 neo4j_query("MATCH (n:`#{mapped_label_name}`) OPTIONAL MATCH (n)-[r]-() DELETE n,r") 114 end
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/active_graph/node/labels.rb 118 def destroy_all 119 all.each(&:destroy) 120 end
Returns the object with the specified neo4j id. @param [String,Integer] id of node to find
# File lib/active_graph/node/labels.rb 89 def find(id) 90 map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object } 91 92 result = find_by_id_or_ids(map_id, id) 93 94 fail RecordNotFound.new( 95 "Couldn't find #{name} with '#{id_property_name}'=#{id.inspect}", 96 name, id_property_name, id) if result.blank? 97 result.tap { |r| find_callbacks!(r) } 98 end
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/active_graph/node/labels.rb 102 def find_by(values) 103 all.where(values).limit(1).query_as(:n).pluck(:n).first 104 end
Like find_by
, except that if no record is found, raises a RecordNotFound
error.
# File lib/active_graph/node/labels.rb 107 def find_by!(values) 108 find_by(values) || fail(RecordNotFound.new("#{self.query_as(:n).where(n: values).limit(1).to_cypher} returned no results", name)) 109 end
@return [ActiveGraph::Label] the label for this class
# File lib/active_graph/node/labels.rb 133 def mapped_label 134 ActiveGraph::Core::Label.new(mapped_label_name) 135 end
@return [Symbol] the label that this class has which corresponds to a Ruby class
# File lib/active_graph/node/labels.rb 128 def mapped_label_name 129 @mapped_label_name || label_for_model 130 end
@return [Array{Symbol}] all the labels that this class has
# File lib/active_graph/node/labels.rb 123 def mapped_label_names 124 self.ancestors.find_all { |a| a.respond_to?(:mapped_label_name) }.map { |a| a.mapped_label_name.to_sym } 125 end
Protected Instance Methods
# File lib/active_graph/node/labels.rb 155 def mapped_label_name=(name) 156 @mapped_label_name = name.to_sym 157 end
# File lib/active_graph/node/labels.rb 151 def mapped_labels 152 mapped_label_names.map { |label_name| ActiveGraph::Label.create(label_name) } 153 end
rubocop:disable Naming/AccessorMethodName
# File lib/active_graph/node/labels.rb 160 def set_mapped_label_name(name) 161 ActiveSupport::Deprecation.warn 'set_mapped_label_name is deprecated, use self.mapped_label_name= instead.', caller 162 163 self.mapped_label_name = name 164 end
Private Instance Methods
# File lib/active_graph/node/labels.rb 192 def decorated_label_name 193 name = case ActiveGraph::Config[:module_handling] 194 when :demodulize 195 self.name.demodulize 196 when Proc 197 ActiveGraph::Config[:module_handling].call self.name 198 else 199 self.name 200 end 201 202 name.to_sym 203 end
rubocop:enable Naming/AccessorMethodName
# File lib/active_graph/node/labels.rb 169 def find_by_id_or_ids(map_id, id) 170 if id.is_a?(Array) 171 find_by_ids(id.map(&map_id)) 172 else 173 find_by_id(map_id.call(id)) 174 end 175 end
# File lib/active_graph/node/labels.rb 177 def find_callbacks!(result) 178 case result 179 when ActiveGraph::Node 180 result.run_callbacks(:find) 181 when Array 182 result.each { |r| find_callbacks!(r) } 183 else 184 result 185 end 186 end
# File lib/active_graph/node/labels.rb 188 def label_for_model 189 (self.name.nil? ? object_id.to_s.to_sym : decorated_label_name) 190 end