lib/neo4j/active_node/labels.rb in neo4j-6.0.0.alpha.8 vs lib/neo4j/active_node/labels.rb in neo4j-6.0.0.alpha.9
- old
+ new
@@ -45,38 +45,36 @@
# @see Neo4j-core
def remove_label(*label)
@_persisted_obj.remove_label(*label)
end
+ def self._wrapped_classes
+ WRAPPED_CLASSES
+ end
+
def self.add_wrapped_class(model)
_wrapped_classes << model
end
- def self._wrapped_classes
- Neo4j::ActiveNode::Labels::WRAPPED_CLASSES
- end
-
+ # Finds an appropriate matching model given a set of labels
+ # which are assigned to a node
def self.model_for_labels(labels)
- MODELS_FOR_LABELS_CACHE[labels] || model_cache(labels)
- end
+ return MODELS_FOR_LABELS_CACHE[labels] if MODELS_FOR_LABELS_CACHE[labels]
- def self.model_cache(labels)
models = WRAPPED_CLASSES.select do |model|
(model.mapped_label_names - labels).size == 0
end
MODELS_FOR_LABELS_CACHE[labels] = models.max do |model|
(model.mapped_label_names & labels).size
end
end
- def self.clear_model_for_label_cache
- MODELS_FOR_LABELS_CACHE.clear
- end
-
def self.clear_wrapped_models
WRAPPED_CLASSES.clear
+ MODELS_FOR_LABELS_CACHE.clear
+ Neo4j::Node::Wrapper::CONSTANTS_FOR_LABELS_CACHE.clear
end
module ClassMethods
include Neo4j::ActiveNode::QueryMethods
@@ -89,11 +87,11 @@
find_by_ids(id.map { |o| map_id.call(o) })
else
find_by_id(map_id.call(id))
end
fail Neo4j::RecordNotFound if result.blank?
- result
+ result.tap { |r| find_callbacks!(r) }
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
def find_by(values)
@@ -161,9 +159,20 @@
self.mapped_label_name = name
end
# rubocop:enable Style/AccessorMethodName
private
+
+ def find_callbacks!(result)
+ case result
+ when Neo4j::ActiveNode
+ result.run_callbacks(:find)
+ when Array
+ result.each { |r| find_callbacks!(r) }
+ else
+ result
+ end
+ end
def label_for_model
(self.name.nil? ? object_id.to_s.to_sym : decorated_label_name)
end