Module: Neo4j::ActiveNode::Labels

Extended by:
ActiveSupport::Concern
Includes:
Index, Reloading
Included in:
Neo4j::ActiveNode
Defined in:
lib/neo4j/active_node/labels.rb,
lib/neo4j/active_node/labels/index.rb,
lib/neo4j/active_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

Reloading::MODELS_TO_RELOAD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reloading

reload_models!

Class Method Details

._wrapped_classesObject



64
65
66
# File 'lib/neo4j/active_node/labels.rb', line 64

def self._wrapped_classes
  WRAPPED_CLASSES
end

.add_wrapped_class(model) ⇒ Object



68
69
70
# File 'lib/neo4j/active_node/labels.rb', line 68

def self.add_wrapped_class(model)
  _wrapped_classes << model
end

.clear_wrapped_modelsObject



87
88
89
90
# File 'lib/neo4j/active_node/labels.rb', line 87

def self.clear_wrapped_models
  MODELS_FOR_LABELS_CACHE.clear
  Neo4j::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



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/neo4j/active_node/labels.rb', line 74

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_label(*_labels) ⇒ Object

Remove this method in 9.0.0



45
46
47
# File 'lib/neo4j/active_node/labels.rb', line 45

def add_label(*_labels)
  fail 'add_label has been removed in favor of `add_labels`'
end

#add_labels(*labels) ⇒ Object

adds one or more labels

See Also:

  • Neo4j-core


36
37
38
39
40
41
42
# File 'lib/neo4j/active_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

#labelsObject

Returns the labels.

Returns:

  • the labels

See Also:

  • Neo4j-core


25
26
27
# File 'lib/neo4j/active_node/labels.rb', line 25

def labels
  @_persisted_obj.labels
end

#remove_label(*_labels) ⇒ Object

Remove this method in 9.0.0



60
61
62
# File 'lib/neo4j/active_node/labels.rb', line 60

def remove_label(*_labels)
  fail 'remove_label has been removed in favor of `remove_labels`'
end

#remove_labels(*labels) ⇒ Object

Removes one or more labels Be careful, don’t remove the label representing the Ruby class.

See Also:

  • Neo4j-core


52
53
54
55
56
57
# File 'lib/neo4j/active_node/labels.rb', line 52

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