Sha256: 360aa2f4128dee8a55f38202e3258ef8142fbd98761565b3607669f47c4dc221
Contents?: true
Size: 1.69 KB
Versions: 2
Compression:
Stored size: 1.69 KB
Contents
module MemoryModel class RecordInvalid < MemoryModel::Error attr_reader :record # :nodoc: def initialize(record) # :nodoc: @record = record errors = @record.errors.full_messages.join(", ") super(errors) end end class Base module Actions extend ActiveSupport::Concern extend ActiveSupport::Autoload autoload :ClassMethods included do define_model_callbacks :create, :update, :save, :destroy, :validation define_model_callbacks :commit, only: :after before_save(:remove_invalid_instance_vars) end VALID_IVARS = [ :@attributes ] def delete self.class.collection.delete(self) freeze end def destroy run_callbacks :destroy do delete end end def save(options={}) !!perform_validations(options) ? commit : false end def save!(options={}) !!perform_validations(options) ? commit : raise(RecordInvalid.new(self)) end private def commit operation = persisted? ? :update : :create run_callbacks operation do run_callbacks :save do self.class.collection.send(operation, self) run_callbacks :commit end end self end def remove_invalid_instance_vars instance_variables.reject { |ivar| ivar.in? VALID_IVARS }.each do |ivar| remove_instance_variable ivar if instance_variable_defined?(ivar) end end def perform_validations(options={}) run_callbacks :validation do options[:validate] == false || valid?(options[:context]) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
memory_model-1.0.0 | lib/memory_model/base/actions.rb |
memory_model-0.1.0 | lib/memory_model/base/actions.rb |