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
52 53 54 |
# File 'lib/active_graph/node/labels.rb', line 52 def self._wrapped_classes WRAPPED_CLASSES end |
.add_wrapped_class(model) ⇒ Object
56 57 58 |
# File 'lib/active_graph/node/labels.rb', line 56 def self.add_wrapped_class(model) _wrapped_classes << model end |
.clear_wrapped_models ⇒ Object
75 76 77 78 |
# File 'lib/active_graph/node/labels.rb', line 75 def self.clear_wrapped_models MODELS_FOR_LABELS_CACHE.clear ActiveGraph::Node::Wrapping::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
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_graph/node/labels.rb', line 62 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
34 35 36 37 38 39 40 |
# File 'lib/active_graph/node/labels.rb', line 34 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.
23 24 25 |
# File 'lib/active_graph/node/labels.rb', line 23 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.
45 46 47 48 49 50 |
# File 'lib/active_graph/node/labels.rb', line 45 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 |