lib/neo4j/shared/persistence.rb in neo4j-4.0.0 vs lib/neo4j/shared/persistence.rb in neo4j-4.1.0
- old
+ new
@@ -1,16 +1,15 @@
module Neo4j::Shared
module Persistence
-
extend ActiveSupport::Concern
include Neo4j::Shared::TypeConverters
USES_CLASSNAME = []
def update_model
if changed_attributes && !changed_attributes.empty?
- changed_props = attributes.select{|k,v| changed_attributes.include?(k)}
+ changed_props = attributes.select { |k, _| changed_attributes.include?(k) }
changed_props = convert_properties_to :db, changed_props
_persisted_obj.update_props(changed_props)
changed_attributes.clear
end
end
@@ -32,37 +31,38 @@
end
def create_or_update
# since the same model can be created or updated twice from a relationship we have to have this guard
@_create_or_updating = true
- result = persisted? ? update_model : create_model
- unless result != false
- Neo4j::Transaction.current.fail if Neo4j::Transaction.current
+ result = _persisted_obj ? update_model : create_model
+ if result == false
+ Neo4j::Transaction.current.failure if Neo4j::Transaction.current
false
else
true
end
rescue => e
- Neo4j::Transaction.current.fail if Neo4j::Transaction.current
+ Neo4j::Transaction.current.failure if Neo4j::Transaction.current
raise e
ensure
@_create_or_updating = nil
end
- # Returns +true+ if the record is persisted, i.e. it’s not a new record and it was not destroyed
+ # Returns +true+ if the record is persisted, i.e. it's not a new record and it was not destroyed
def persisted?
!new_record? && !destroyed?
end
# Returns +true+ if the record hasn't been saved to Neo4j yet.
def new_record?
!_persisted_obj
end
- alias :new? :new_record?
+ alias_method :new?, :new_record?
def destroy
+ freeze
_persisted_obj && _persisted_obj.del
@_deleted = true
end
def exist?
@@ -75,34 +75,23 @@
end
# @return [Hash] all defined and none nil properties
def props
- attributes.reject{|k,v| v.nil?}.symbolize_keys
+ attributes.reject { |_, v| v.nil? }.symbolize_keys
end
# @return true if the attributes hash has been frozen
def frozen?
- freeze_if_deleted
@attributes.frozen?
end
def freeze
@attributes.freeze
self
end
- def freeze_if_deleted
- unless new_record?
- # TODO - Neo4j::IdentityMap.remove_node_by_id(neo_id)
- unless self.class.load_entity(neo_id)
- @_deleted = true
- freeze
- end
- end
- end
-
def reload
return self if new_record?
clear_association_cache
changed_attributes && changed_attributes.clear
unless reload_from_database
@@ -111,10 +100,10 @@
end
self
end
def reload_from_database
- # TODO - Neo4j::IdentityMap.remove_node_by_id(neo_id)
+ # TODO: - Neo4j::IdentityMap.remove_node_by_id(neo_id)
if reloaded = self.class.load_entity(neo_id)
send(:attributes=, reloaded.attributes)
end
reloaded
end