lib/neo4j/active_node/labels.rb in neo4j-7.2.3 vs lib/neo4j/active_node/labels.rb in neo4j-8.0.0.alpha.1
- old
+ new
@@ -1,5 +1,7 @@
+require 'neo4j/core/label'
+
module Neo4j
module ActiveNode
# Provides a mapping between neo4j labels and Ruby classes
module Labels
extend ActiveSupport::Concern
@@ -9,12 +11,16 @@
WRAPPED_CLASSES = []
MODELS_FOR_LABELS_CACHE = {}
MODELS_FOR_LABELS_CACHE.clear
included do |model|
- Neo4j::ActiveNode::Labels.clear_wrapped_models
+ def self.inherited(model)
+ add_wrapped_class(model)
+ super
+ end
+
Neo4j::ActiveNode::Labels.add_wrapped_class(model) unless Neo4j::ActiveNode::Labels._wrapped_classes.include?(model)
end
class RecordNotFound < Neo4j::RecordNotFound; end
@@ -29,21 +35,38 @@
# self.class.query_as(:result).where("ID(result)" => self.neo_id).return("LABELS(result) as result_labels").first.result_labels.map(&:to_sym)
# end
# adds one or more labels
# @see Neo4j-core
- def add_label(*label)
- @_persisted_obj.add_label(*label)
+ def add_labels(*labels)
+ labels.inject(query_as(:n)) do |query, label|
+ query.set("n:`#{label}`")
+ end.exec
+ @_persisted_obj.labels.concat(labels)
+ @_persisted_obj.labels.uniq!
end
+ # Remove this method in 9.0.0
+ def add_label(*_labels)
+ fail 'add_label has been removed in favor of `add_labels`'
+ end
+
# Removes one or more labels
# Be careful, don't remove the label representing the Ruby class.
# @see Neo4j-core
- def remove_label(*label)
- @_persisted_obj.remove_label(*label)
+ def remove_labels(*labels)
+ labels.inject(query_as(:n)) do |query, label|
+ query.remove("n:`#{label}`")
+ end.exec
+ labels.each(&@_persisted_obj.labels.method(:delete))
end
+ # Remove this method in 9.0.0
+ def remove_label(*_labels)
+ fail 'remove_label has been removed in favor of `remove_labels`'
+ end
+
def self._wrapped_classes
WRAPPED_CLASSES
end
def self.add_wrapped_class(model)
@@ -54,21 +77,21 @@
# which are assigned to a node
def self.model_for_labels(labels)
return MODELS_FOR_LABELS_CACHE[labels] if MODELS_FOR_LABELS_CACHE[labels]
models = WRAPPED_CLASSES.select do |model|
- (model.mapped_label_names - labels).size == 0
+ (model.mapped_label_names - labels).empty?
end
MODELS_FOR_LABELS_CACHE[labels] = models.max_by do |model|
(model.mapped_label_names & labels).size
end
end
def self.clear_wrapped_models
MODELS_FOR_LABELS_CACHE.clear
- Neo4j::Node::Wrapper::CONSTANTS_FOR_LABELS_CACHE.clear
+ Neo4j::NodeWrapping::CONSTANTS_FOR_LABELS_CACHE.clear
end
module ClassMethods
include Neo4j::ActiveNode::QueryMethods
@@ -98,12 +121,11 @@
find_by(values) || fail(RecordNotFound, "#{self.query_as(:n).where(n: values).limit(1).to_cypher} returned no results")
end
# Deletes all nodes and connected relationships from Cypher.
def delete_all
- self.neo4j_session._query("MATCH (n:`#{mapped_label_name}`) OPTIONAL MATCH (n)-[r]-() DELETE n,r")
- self.neo4j_session._query("MATCH (n:`#{mapped_label_name}`) DELETE n")
+ neo4j_query("MATCH (n:`#{mapped_label_name}`) OPTIONAL MATCH (n)-[r]-() DELETE n,r")
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.
def destroy_all
@@ -120,10 +142,10 @@
@mapped_label_name || label_for_model
end
# @return [Neo4j::Label] the label for this class
def mapped_label
- Neo4j::Label.create(mapped_label_name)
+ Neo4j::Core::Label.new(mapped_label_name, neo4j_session)
end
def base_class
unless self < Neo4j::ActiveNode
fail "#{name} doesn't belong in a hierarchy descending from ActiveNode"