Persistence¶
Constants¶
Methods¶
def apply_default_values return if self.class.declared_property_defaults.empty? self.class.declared_property_defaults.each_pair do |key, value| self.send("#{key}=", value) if self.send(key).nil? end end
def cache_key if self.new_record? "#{model_cache_key}/new" elsif self.respond_to?(:updated_at) && !self.updated_at.blank? "#{model_cache_key}/#{neo_id}-#{self.updated_at.utc.to_s(:number)}" else "#{model_cache_key}/#{neo_id}" end 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 apply_default_values 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.failure if Neo4j::Transaction.current raise e ensure @_create_or_updating = nil end
def destroy freeze _persisted_obj && _persisted_obj.del @_deleted = true end
def exist? _persisted_obj && _persisted_obj.exist? end
def freeze @attributes.freeze self end
def frozen? @attributes.frozen? end
- #increment!
Convenience method to increment numeric attribute and #save at the same time
def increment!(attribute, by = 1) increment(attribute, by).update_attribute(attribute, self[attribute]) end
def props attributes.reject { |_, v| v.nil? }.symbolize_keys end
def props_for_persistence _persisted_obj ? props_for_update : props_for_create end
def props_for_update update_magic_properties changed_props = attributes.select { |k, _| changed_attributes.include?(k) } changed_props.symbolize_keys! inject_defaults!(changed_props) props_for_db(changed_props) end
def reload return self if new_record? association_proxy_cache.clear if respond_to?(:association_proxy_cache) changed_attributes && changed_attributes.clear unless reload_from_database @_deleted = true freeze end self end
def reload_from_database reloaded = self.class.load_entity(neo_id) reloaded ? init_on_reload(reloaded._persisted_obj) : nil end
def touch fail 'Cannot touch on a new record object' unless persisted? update_attribute!(:updated_at, Time.now) if respond_to?(:updated_at=) end
- #update!
Same as {#update_attributes}, but raises an exception if saving fails.
def update!(attributes) self.attributes = process_attributes(attributes) save! end
- #update_attribute!
Convenience method to set attribute and #save! at the same time
def update_attribute!(attribute, value) send("#{attribute}=", value) self.save! end
- #update_attributes!
Same as {#update_attributes}, but raises an exception if saving fails.
def update!(attributes) self.attributes = process_attributes(attributes) save! end
def update_model return if !changed_attributes || changed_attributes.empty? _persisted_obj.update_props(props_for_update) changed_attributes.clear end