module ActiveGraph::Shared::Persistence
rubocop:disable Metrics/ModuleLength
Public Instance Methods
# File lib/active_graph/shared/persistence.rb 100 def apply_default_values 101 return if self.class.declared_property_defaults.empty? 102 self.class.declared_property_defaults.each_pair do |key, value| 103 self.send("#{key}=", value.respond_to?(:call) ? value.call : value) if self.send(key).nil? 104 end 105 end
# File lib/active_graph/shared/persistence.rb 216 def cache_key 217 if self.new_record? 218 "#{model_cache_key}/new" 219 elsif self.respond_to?(:updated_at) && !self.updated_at.blank? 220 "#{model_cache_key}/#{neo_id}-#{self.updated_at.utc.to_s(:number)}" 221 else 222 "#{model_cache_key}/#{neo_id}" 223 end 224 end
Increments concurrently a numeric attribute by a centain amount @param [Symbol, String] _attribute name of the attribute to increment @param [Integer, Float] _by amount to increment
# File lib/active_graph/shared/persistence.rb 67 def concurrent_increment!(_attribute, _by = 1) 68 fail 'not_implemented' 69 end
# File lib/active_graph/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 93 ActiveGraph::Base.transaction(&:failure) if result == false 94 95 result != false 96 ensure 97 @_create_or_updating = nil 98 end
# File lib/active_graph/shared/persistence.rb 124 def destroy 125 freeze 126 127 destroy_query.exec if _persisted_obj 128 129 @_deleted = true 130 131 self 132 end
Returns true
if the object was destroyed.
# File lib/active_graph/shared/persistence.rb 141 def destroyed? 142 @_deleted 143 end
# File lib/active_graph/shared/persistence.rb 134 def exist? 135 return if !_persisted_obj 136 137 neo4j_query(query_as(:n).return('ID(n)')).any? 138 end
# File lib/active_graph/shared/persistence.rb 155 def freeze 156 @attributes.freeze 157 self 158 end
@return true if the attributes hash has been frozen
# File lib/active_graph/shared/persistence.rb 151 def frozen? 152 @attributes.frozen? 153 end
Increments a numeric attribute by a centain amount @param [Symbol, String] attribute name of the attribute to increment @param [Integer, Float] by amount to increment
# File lib/active_graph/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] attribute name of the attribute to increment @param [Integer, Float] by amount to increment
# File lib/active_graph/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/active_graph/shared/persistence.rb 118 def new_record? 119 !_persisted_obj 120 end
Returns true
if the record is persisted, i.e. it's not a new record and it was not destroyed
# File lib/active_graph/shared/persistence.rb 113 def persisted? 114 !new_record? && !destroyed? 115 end
@return [Hash] all defined and none nil properties
# File lib/active_graph/shared/persistence.rb 146 def props 147 attributes.reject { |_, v| v.nil? }.symbolize_keys 148 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/active_graph/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/active_graph/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/active_graph/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/active_graph/shared/persistence.rb 160 def reload 161 return self if new_record? 162 association_proxy_cache.clear if respond_to?(:association_proxy_cache) 163 changed_attributes_clear! 164 unless reload_from_database 165 @_deleted = true 166 freeze 167 end 168 self 169 end
# File lib/active_graph/shared/persistence.rb 171 def reload_from_database 172 reloaded = self.class.load_entity(neo_id) 173 reloaded ? init_on_reload(reloaded._persisted_obj) : nil 174 end
# File lib/active_graph/shared/persistence.rb 20 def skip_update? 21 changed_attributes.blank? 22 end
# File lib/active_graph/shared/persistence.rb 107 def touch 108 fail 'Cannot touch on a new record object' unless persisted? 109 update_attribute!(:updated_at, Time.now) if respond_to?(:updated_at=) 110 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/active_graph/shared/persistence.rb 178 def update(attributes) 179 ActiveGraph::Base.transaction do |tx| 180 self.attributes = process_attributes(attributes) 181 saved = save 182 tx.failure unless saved 183 saved 184 end 185 end
Same as {#update_attributes}, but raises an exception if saving fails.
# File lib/active_graph/shared/persistence.rb 208 def update!(attributes) 209 ActiveGraph::Base.transaction do 210 self.attributes = process_attributes(attributes) 211 save! 212 end 213 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/active_graph/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/active_graph/shared/persistence.rb 82 def update_attribute!(attribute, value) 83 write_attribute(attribute, value) 84 self.save! 85 end
# File lib/active_graph/shared/persistence.rb 194 def update_db_properties(hash) 195 fail ::ActiveGraph::Error, 'can not update on a new record object' unless persisted? 196 ActiveGraph::Base.transaction do 197 db_values = props_for_db(hash) 198 neo4j_query(query_as(:n).set(n: db_values)) 199 db_values.each_pair { |k, v| self.public_send(:"#{k}=", v) } 200 _persisted_obj.properties.merge!(db_values) 201 changed_attributes_selective_clear!(db_values) 202 true 203 end 204 end
# File lib/active_graph/shared/persistence.rb 188 def update_db_property(field, value) 189 update_db_properties(field => value) 190 true 191 end
# File lib/active_graph/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.properties.merge!(props) 17 changed_attributes_clear! 18 end
Protected Instance Methods
# File lib/active_graph/shared/persistence.rb 228 def increment_by_query!(match_query, attribute, by, element_name = :n) 229 new_attribute = match_query.with(element_name) 230 .set("#{element_name}.`#{attribute}` = COALESCE(#{element_name}.`#{attribute}`, 0) + $by") 231 .params(by: by).limit(1) 232 .pluck("#{element_name}.`#{attribute}`").first 233 return false unless new_attribute 234 self[attribute] = new_attribute 235 236 if defined? ActiveModel::ForcedMutationTracker 237 # with ActiveModel 6.0.0 set_attribute_was is removed 238 # so we mark attribute's previous value using attr_will_change method 239 clear_attribute_change(attribute) 240 else 241 set_attribute_was(attribute, new_attribute) 242 end 243 true 244 end
Private Instance Methods
# File lib/active_graph/shared/persistence.rb 260 def inject_timestamps! 261 now = DateTime.now 262 self.created_at ||= now if respond_to?(:created_at=) 263 self.updated_at ||= now if respond_to?(:updated_at=) 264 end
# File lib/active_graph/shared/persistence.rb 252 def model_cache_key 253 self.class.model_name.cache_key 254 end
# File lib/active_graph/shared/persistence.rb 248 def props_for_db(props_hash) 249 self.class.declared_properties.convert_properties_to(self, :db, props_hash) 250 end
# File lib/active_graph/shared/persistence.rb 266 def set_timestamps 267 warning = 'This method has been replaced with `inject_timestamps!` and will be removed in a future version'.freeze 268 ActiveSupport::Deprecation.warn warning, caller 269 inject_timestamps! 270 end
# File lib/active_graph/shared/persistence.rb 256 def update_magic_properties 257 self.updated_at = DateTime.now if respond_to?(:updated_at=) && (updated_at.nil? || (changed? && !updated_at_changed?)) 258 end