module Neo4j::Shared::Persistence
rubocop:disable Metrics/ModuleLength
Public Instance Methods
# File lib/neo4j/shared/persistence.rb 104 def apply_default_values 105 return if self.class.declared_property_defaults.empty? 106 self.class.declared_property_defaults.each_pair do |key, value| 107 self.send("#{key}=", value.respond_to?(:call) ? value.call : value) if self.send(key).nil? 108 end 109 end
# File lib/neo4j/shared/persistence.rb 220 def cache_key 221 if self.new_record? 222 "#{model_cache_key}/new" 223 elsif self.respond_to?(:updated_at) && !self.updated_at.blank? 224 "#{model_cache_key}/#{neo_id}-#{self.updated_at.utc.to_s(:number)}" 225 else 226 "#{model_cache_key}/#{neo_id}" 227 end 228 end
Increments concurrently a numeric attribute by a centain amount @param [Symbol, String] name of the attribute to increment @param [Integer, Float] amount to increment
# File lib/neo4j/shared/persistence.rb 67 def concurrent_increment!(_attribute, _by = 1) 68 fail 'not_implemented' 69 end
# File lib/neo4j/shared/persistence.rb 87 def create_or_update 88 # since the same model can be created or updated twice from a relationship we have to have this guard 89 @_create_or_updating = true 90 apply_default_values 91 result = _persisted_obj ? update_model : create_model 92 current_transaction = Neo4j::ActiveBase.current_transaction 93 94 current_transaction.mark_failed if result == false && current_transaction 95 96 result != false 97 rescue => e 98 current_transaction.mark_failed if current_transaction 99 raise e 100 ensure 101 @_create_or_updating = nil 102 end
# File lib/neo4j/shared/persistence.rb 128 def destroy 129 freeze 130 131 destroy_query.exec if _persisted_obj 132 133 @_deleted = true 134 135 self 136 end
Returns true
if the object was destroyed.
# File lib/neo4j/shared/persistence.rb 145 def destroyed? 146 @_deleted 147 end
# File lib/neo4j/shared/persistence.rb 138 def exist? 139 return if !_persisted_obj 140 141 neo4j_query(query_as(:n).return('ID(n)')).any? 142 end
# File lib/neo4j/shared/persistence.rb 159 def freeze 160 @attributes.freeze 161 self 162 end
@return true if the attributes hash has been frozen
# File lib/neo4j/shared/persistence.rb 155 def frozen? 156 @attributes.frozen? 157 end
Increments a numeric attribute by a centain amount @param [Symbol, String] name of the attribute to increment @param [Integer, Float] amount to increment
# File lib/neo4j/shared/persistence.rb 51 def increment(attribute, by = 1) 52 self[attribute] ||= 0 53 self[attribute] += by 54 self 55 end
Convenience method to increment numeric attribute and save at the same time @param [Symbol, String] name of the attribute to increment @param [Integer, Float] amount to increment
# File lib/neo4j/shared/persistence.rb 60 def increment!(attribute, by = 1) 61 increment(attribute, by).update_attribute(attribute, self[attribute]) 62 end
Returns true
if the record hasn't been saved to Neo4j yet.
# File lib/neo4j/shared/persistence.rb 122 def new_record? 123 !_persisted_obj 124 end
Returns true
if the record is persisted, i.e. it's not a new record and it was not destroyed
# File lib/neo4j/shared/persistence.rb 117 def persisted? 118 !new_record? && !destroyed? 119 end
@return [Hash] all defined and none nil properties
# File lib/neo4j/shared/persistence.rb 150 def props 151 attributes.reject { |_, v| v.nil? }.symbolize_keys 152 end
Returns a hash containing:
-
All properties and values for insertion in the database
-
A `uuid` (or equivalent) key and value
-
Timestamps
, if the class is set to include them.
Note that the UUID is added to the hash but is not set on the node. The timestamps, by comparison, are set on the node prior to addition in this hash. @return [Hash]
# File lib/neo4j/shared/persistence.rb 31 def props_for_create 32 inject_timestamps! 33 props_with_defaults = inject_defaults!(props) 34 converted_props = props_for_db(props_with_defaults) 35 return converted_props unless self.class.respond_to?(:default_property_values) 36 inject_primary_key!(converted_props) 37 end
@return [Hash] Given a node's state, will call the appropriate `props_for_{action}` method.
# File lib/neo4j/shared/persistence.rb 8 def props_for_persistence 9 _persisted_obj ? props_for_update : props_for_create 10 end
@return [Hash] Properties and values, type-converted and timestamped for the database.
# File lib/neo4j/shared/persistence.rb 40 def props_for_update 41 update_magic_properties 42 changed_props = attributes.select { |k, _| changed_attributes.include?(k) } 43 changed_props.symbolize_keys! 44 inject_defaults!(changed_props) 45 props_for_db(changed_props) 46 end
# File lib/neo4j/shared/persistence.rb 164 def reload 165 return self if new_record? 166 association_proxy_cache.clear if respond_to?(:association_proxy_cache) 167 changed_attributes_clear! 168 unless reload_from_database 169 @_deleted = true 170 freeze 171 end 172 self 173 end
# File lib/neo4j/shared/persistence.rb 175 def reload_from_database 176 reloaded = self.class.load_entity(neo_id) 177 reloaded ? init_on_reload(reloaded._persisted_obj) : nil 178 end
# File lib/neo4j/shared/persistence.rb 20 def skip_update? 21 changed_attributes.blank? 22 end
# File lib/neo4j/shared/persistence.rb 111 def touch 112 fail 'Cannot touch on a new record object' unless persisted? 113 update_attribute!(:updated_at, Time.now) if respond_to?(:updated_at=) 114 end
Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved. If saving fails because the resource is invalid then false will be returned.
# File lib/neo4j/shared/persistence.rb 182 def update(attributes) 183 self.class.run_transaction do |tx| 184 self.attributes = process_attributes(attributes) 185 saved = save 186 tx.mark_failed unless saved 187 saved 188 end 189 end
Same as {#update_attributes}, but raises an exception if saving fails.
# File lib/neo4j/shared/persistence.rb 212 def update!(attributes) 213 self.class.run_transaction do 214 self.attributes = process_attributes(attributes) 215 save! 216 end 217 end
Convenience method to set attribute and save at the same time @param [Symbol, String] attribute of the attribute to update @param [Object] value to set
# File lib/neo4j/shared/persistence.rb 74 def update_attribute(attribute, value) 75 write_attribute(attribute, value) 76 self.save 77 end
Convenience method to set attribute and save! at the same time @param [Symbol, String] attribute of the attribute to update @param [Object] value to set
# File lib/neo4j/shared/persistence.rb 82 def update_attribute!(attribute, value) 83 write_attribute(attribute, value) 84 self.save! 85 end
# File lib/neo4j/shared/persistence.rb 198 def update_db_properties(hash) 199 fail ::Neo4j::Error, 'can not update on a new record object' unless persisted? 200 self.class.run_transaction do 201 db_values = props_for_db(hash) 202 neo4j_query(query_as(:n).set(n: db_values)) 203 db_values.each_pair { |k, v| self.public_send(:"#{k}=", v) } 204 _persisted_obj.props.merge!(db_values) 205 changed_attributes_selective_clear!(db_values) 206 true 207 end 208 end
# File lib/neo4j/shared/persistence.rb 192 def update_db_property(field, value) 193 update_db_properties(field => value) 194 true 195 end
# File lib/neo4j/shared/persistence.rb 12 def update_model 13 return if skip_update? 14 props = props_for_update 15 neo4j_query(query_as(:n).set(n: props)) 16 _persisted_obj.props.merge!(props) 17 changed_attributes_clear! 18 end
Protected Instance Methods
# File lib/neo4j/shared/persistence.rb 238 def increment_by_query!(match_query, attribute, by, element_name = :n) 239 new_attribute = match_query.with(element_name) 240 .set("#{element_name}.`#{attribute}` = COALESCE(#{element_name}.`#{attribute}`, 0) + {by}") 241 .params(by: by).limit(1) 242 .pluck("#{element_name}.`#{attribute}`").first 243 return false unless new_attribute 244 self[attribute] = new_attribute 245 246 if defined? ActiveModel::ForcedMutationTracker 247 # with ActiveModel 6.0.0 set_attribute_was is removed 248 # so we mark attribute's previous value using attr_will_change method 249 clear_attribute_change(attribute) 250 else 251 set_attribute_was(attribute, new_attribute) 252 end 253 true 254 end
Private Instance Methods
# File lib/neo4j/shared/persistence.rb 270 def inject_timestamps! 271 now = DateTime.now 272 self.created_at ||= now if respond_to?(:created_at=) 273 self.updated_at ||= now if respond_to?(:updated_at=) 274 end
# File lib/neo4j/shared/persistence.rb 262 def model_cache_key 263 self.class.model_name.cache_key 264 end
# File lib/neo4j/shared/persistence.rb 258 def props_for_db(props_hash) 259 self.class.declared_properties.convert_properties_to(self, :db, props_hash) 260 end
# File lib/neo4j/shared/persistence.rb 276 def set_timestamps 277 warning = 'This method has been replaced with `inject_timestamps!` and will be removed in a future version'.freeze 278 ActiveSupport::Deprecation.warn warning, caller 279 inject_timestamps! 280 end
# File lib/neo4j/shared/persistence.rb 266 def update_magic_properties 267 self.updated_at = DateTime.now if respond_to?(:updated_at=) && (updated_at.nil? || (changed? && !updated_at_changed?)) 268 end