lib/neo4j/active_node/labels.rb in neo4j-5.2.15 vs lib/neo4j/active_node/labels.rb in neo4j-6.0.0.alpha.1

- old
+ new

@@ -1,10 +1,11 @@ module Neo4j module ActiveNode # Provides a mapping between neo4j labels and Ruby classes module Labels extend ActiveSupport::Concern + include Neo4j::ActiveNode::Labels::Index include Neo4j::ActiveNode::Labels::Reloading WRAPPED_CLASSES = [] MODELS_FOR_LABELS_CACHE = {} MODELS_FOR_LABELS_CACHE.clear @@ -82,17 +83,17 @@ # Returns the object with the specified neo4j id. # @param [String,Integer] id of node to find def find(id) map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object } - result = if id.is_a?(Array) - find_by_ids(id.map { |o| map_id.call(o) }) - else - find_by_id(map_id.call(id)) - end + result = if id.is_a?(Array) + 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.tap { |r| find_callbacks!(r) } + result 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) @@ -114,75 +115,10 @@ # one database query per node in the database, more if callbacks require them. def destroy_all all.each(&:destroy) end - # Creates a Neo4j index on given property - # - # This can also be done on the property directly, see Neo4j::ActiveNode::Property::ClassMethods#property. - # - # @param [Symbol] property the property we want a Neo4j index on - # @param [Hash] conf optional property configuration - # - # @example - # class Person - # include Neo4j::ActiveNode - # property :name - # index :name - # end - # - # @example with constraint - # class Person - # include Neo4j::ActiveNode - # property :name - # - # # below is same as: index :name, index: :exact, constraint: {type: :unique} - # index :name, constraint: {type: :unique} - # end - def index(property, conf = {}) - Neo4j::Session.on_session_available do |_| - drop_constraint(property, type: :unique) if Neo4j::Label.constraint?(mapped_label_name, property) - _index(property, conf) - end - indexed_properties.push property unless indexed_properties.include? property - end - - # Creates a neo4j constraint on this class for given property - # - # @example - # Person.constraint :name, type: :unique - # - def constraint(property, constraints) - Neo4j::Session.on_session_available do |session| - unless Neo4j::Label.constraint?(mapped_label_name, property) - label = Neo4j::Label.create(mapped_label_name) - drop_index(property, label) if index?(property) - label.create_constraint(property, constraints, session) - end - end - end - - # @param [Symbol] property The name of the property index to be dropped - # @param [Neo4j::Label] label An instance of label from Neo4j::Core - def drop_index(property, label = nil) - label_obj = label || Neo4j::Label.create(mapped_label_name) - label_obj.drop_index(property) - end - - # @param [Symbol] property The name of the property constraint to be dropped - # @param [Hash] constraint The constraint type to be dropped. - def drop_constraint(property, constraint = {type: :unique}) - Neo4j::Session.on_session_available do |session| - label = Neo4j::Label.create(mapped_label_name) - label.drop_constraint(property, constraint, session) - end - end - - def index?(index_def) - mapped_label.indexes[:property_keys].include?([index_def]) - end - # @return [Array{Symbol}] all the labels that this class has def mapped_label_names self.ancestors.find_all { |a| a.respond_to?(:mapped_label_name) }.map { |a| a.mapped_label_name.to_sym } end @@ -194,14 +130,10 @@ # @return [Neo4j::Label] the label for this class def mapped_label Neo4j::Label.create(mapped_label_name) end - def indexed_properties - @_indexed_properties ||= [] - end - def base_class unless self < Neo4j::ActiveNode fail "#{name} doesn't belong in a hierarchy descending from ActiveNode" end @@ -210,27 +142,12 @@ else superclass.base_class end end - protected - def _index(property, conf) - mapped_labels.each do |label| - # make sure the property is not indexed twice - existing = label.indexes[:property_keys] - - # In neo4j constraint automatically creates an index - if conf[:constraint] - constraint(property, conf[:constraint]) - else - label.create_index(property) unless existing.flatten.include?(property) - end - end - end - def mapped_labels mapped_label_names.map { |label_name| Neo4j::Label.create(label_name) } end def mapped_label_name=(name) @@ -244,20 +161,9 @@ 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