Module: ActiveGraph::Node::Labels
- Extended by:
- ActiveSupport::Concern
- Included in:
- ActiveGraph::Node
- Defined in:
- lib/active_graph/node/labels.rb,
lib/active_graph/node/labels/index.rb,
lib/active_graph/node/labels/reloading.rb
Overview
Provides a mapping between neo4j labels and Ruby classes
Defined Under Namespace
Modules: ClassMethods, Index, Reloading Classes: RecordNotFound
Constant Summary collapse
- WRAPPED_CLASSES =
[]
- MODELS_FOR_LABELS_CACHE =
{}
Constants included from Reloading
Class Method Summary collapse
- ._wrapped_classes ⇒ Object
- .add_wrapped_class(model) ⇒ Object
- .clear_wrapped_models ⇒ Object
-
.model_for_labels(labels) ⇒ Object
Finds an appropriate matching model given a set of labels which are assigned to a node.
Instance Method Summary collapse
-
#add_labels(*labels) ⇒ Object
adds one or more labels.
-
#labels ⇒ Object
The labels.
-
#remove_labels(*labels) ⇒ Object
Removes one or more labels Be careful, don't remove the label representing the Ruby class.
Methods included from Reloading
Class Method Details
._wrapped_classes ⇒ Object
54 55 56 |
# File 'lib/active_graph/node/labels.rb', line 54 def self._wrapped_classes WRAPPED_CLASSES end |
.add_wrapped_class(model) ⇒ Object
58 59 60 |
# File 'lib/active_graph/node/labels.rb', line 58 def self.add_wrapped_class(model) _wrapped_classes << model end |
.clear_wrapped_models ⇒ Object
77 78 79 80 |
# File 'lib/active_graph/node/labels.rb', line 77 def self.clear_wrapped_models MODELS_FOR_LABELS_CACHE.clear ActiveGraph::NodeWrapping::CONSTANTS_FOR_LABELS_CACHE.clear end |
.model_for_labels(labels) ⇒ Object
Finds an appropriate matching model given a set of labels which are assigned to a node
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_graph/node/labels.rb', line 64 def self.model_for_labels(labels) labels.sort! return MODELS_FOR_LABELS_CACHE[labels] if MODELS_FOR_LABELS_CACHE[labels] models = WRAPPED_CLASSES.select do |model| (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 |
Instance Method Details
#add_labels(*labels) ⇒ Object
adds one or more labels
36 37 38 39 40 41 42 |
# File 'lib/active_graph/node/labels.rb', line 36 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 |
#labels ⇒ Object
Returns the labels.
25 26 27 |
# File 'lib/active_graph/node/labels.rb', line 25 def labels @_persisted_obj.labels end |
#remove_labels(*labels) ⇒ Object
Removes one or more labels Be careful, don't remove the label representing the Ruby class.
47 48 49 50 51 52 |
# File 'lib/active_graph/node/labels.rb', line 47 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 |