# File lib/motion_record/persistence.rb, line 64 def primary_key_condition {self.class.primary_key => self.instance_variable_get("@#{self.class.primary_key}")} end
module MotionRecord::Persistence
Constants
- TIMESTAMP_COLUMNS
Protected Class Methods
included(base)
click to toggle source
# File lib/motion_record/persistence.rb, line 121 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
destroy()
click to toggle source
# File lib/motion_record/persistence.rb, line 10 def destroy delete! end
mark_persisted!()
click to toggle source
# File lib/motion_record/persistence.rb, line 18 def mark_persisted! @persisted = true end
mark_unpersisted!()
click to toggle source
# File lib/motion_record/persistence.rb, line 22 def mark_unpersisted! @persisted = false end
persisted?()
click to toggle source
# File lib/motion_record/persistence.rb, line 14 def persisted? !!@persisted end
save()
click to toggle source
# File lib/motion_record/persistence.rb, line 6 def save persist! end
Protected Instance Methods
apply_persistence_timestamps()
click to toggle source
Update persistence auto-timestamp attributes
# File lib/motion_record/persistence.rb, line 59 def apply_persistence_timestamps self.updated_at = Time.now if self.class.attribute_names.include?(:updated_at) self.created_at ||= Time.now if self.class.attribute_names.include?(:created_at) end
delete!()
click to toggle source
# File lib/motion_record/persistence.rb, line 50 def delete! if persisted? self.class.where(primary_key_condition).delete_all else raise "Can't delete unpersisted records" end end
persist!()
click to toggle source
# File lib/motion_record/persistence.rb, line 28 def persist! # HACK: Must ensure that attribute definitions are loaded from the table self.class.table_columns self.apply_persistence_timestamps params = self.to_attribute_hash.reject { |k, _v| k == self.class.primary_key } table_params = self.class.serialize_table_params(params) if persisted? self.class.where(primary_key_condition).update_all(table_params) else connection.insert self.class.table_name, table_params if self.class.primary_key # HACK: This assumes that primary keys are monotonically increasing newest_primary_key = self.class.maximum(self.class.primary_key) self.self.instance_variable_set "@#{self.class.primary_key}", newest_primary_key end end self.mark_persisted! end
primary_key_condition()
click to toggle source