lib/neo4j/active_node/labels.rb in neo4j-4.0.0 vs lib/neo4j/active_node/labels.rb in neo4j-4.1.0

- old
+ new

@@ -1,9 +1,7 @@ module Neo4j module ActiveNode - - # Provides a mapping between neo4j labels and Ruby classes module Labels extend ActiveSupport::Concern WRAPPED_CLASSES = [] @@ -50,11 +48,11 @@ protected # Only for testing purpose # @private def self._wrapped_labels=(wl) - @_wrapped_labels=(wl) + @_wrapped_labels = (wl) end def self._wrapped_labels @_wrapped_labels ||= _wrapped_classes.inject({}) do |ack, clazz| ack.tap do |a| @@ -72,29 +70,28 @@ end # Returns the object with the specified neo4j id. # @param [String,Fixnum] id of node to find def find(id) - map_id = Proc.new {|object| object.respond_to?(:id) ? object.send(:id) : object } + map_id = proc { |object| object.respond_to?(:id) ? object.send(:id) : object } if id.is_a?(Array) - find_by_ids(id.map {|o| map_id.call(o) }) + find_by_ids(id.map { |o| map_id.call(o) }) else find_by_id(map_id.call(id)) end end # Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself. - # @param [Hash] args of arguments to find - def find_by(*args) - self.query_as(:n).where(n: eval(args.join)).limit(1).pluck(:n).first + # @param Hash args of arguments to find + def find_by(values) + self.query_as(:n).where(n: values).limit(1).pluck(:n).first end # Like find_by, except that if no record is found, raises a RecordNotFound error. - def find_by!(*args) - a = eval(args.join) - find_by(args) or raise RecordNotFound, "#{self.query_as(:n).where(n: a).limit(1).to_cypher} returned no results" + def find_by!(values) + find_by(values) || fail(RecordNotFound, "#{self.query_as(:n).where(n: values).limit(1).to_cypher} returned no results") end # Deletes all nodes and connected relationships from Cypher. def delete_all self.neo4j_session._query("MATCH (n:`#{mapped_label_name}`)-[r]-() DELETE n,r") @@ -102,11 +99,11 @@ end # Returns each node to Ruby and calls `destroy`. Be careful, as this can be a very slow operation if you have many nodes. It will generate at least # one database query per node in the database, more if callbacks require them. def destroy_all - self.all.each { |n| n.destroy } + self.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. @@ -139,18 +136,18 @@ # Creates a neo4j constraint on this class for given property # # @example # Person.constraint :name, type: :unique # - def constraint(property, constraints, session = Neo4j::Session.current) + def constraint(property, constraints) Neo4j::Session.on_session_available do |session| label = Neo4j::Label.create(mapped_label_name) label.create_constraint(property, constraints, session) end end - def drop_constraint(property, constraint, session = Neo4j::Session.current) + def drop_constraint(property, constraint) Neo4j::Session.on_session_available do |session| label = Neo4j::Label.create(mapped_label_name) label.drop_constraint(property, constraint, session) end end @@ -164,11 +161,11 @@ self.ancestors.find_all { |a| a.respond_to?(:mapped_label_name) }.map { |a| a.mapped_label_name.to_sym } end # @return [Symbol] the label that this class has which corresponds to a Ruby class def mapped_label_name - @_label_name || (self.name.nil? ? object_id.to_s.to_sym : self.name.to_sym) + @mapped_label_name || (self.name.nil? ? object_id.to_s.to_sym : self.name.to_sym) end # @return [Neo4j::Label] the label for this class def mapped_label Neo4j::Label.create(mapped_label_name) @@ -178,11 +175,11 @@ @_indexed_properties ||= [] end def base_class unless self < Neo4j::ActiveNode - raise "#{name} doesn't belong in a hierarchy descending from ActiveNode" + fail "#{name} doesn't belong in a hierarchy descending from ActiveNode" end if superclass == Object self else @@ -202,23 +199,27 @@ 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)} + mapped_label_names.map { |label_name| Neo4j::Label.create(label_name) } end - def set_mapped_label_name(name) - @_label_name = name.to_sym + def mapped_label_name=(name) + @mapped_label_name = name.to_sym end - end + # rubocop:disable Style/AccessorMethodName + def set_mapped_label_name(name) + ActiveSupport::Deprecation.warn 'set_mapped_label_name is deprecated and may be removed from future releases, use self.mapped_label_name= instead.', caller + self.mapped_label_name = name + end + # rubocop:enable Style/AccessorMethodName + end end - end end