Class: SimpleModel::Base
- Inherits:
-
Object
- Object
- SimpleModel::Base
- Includes:
- Attributes, ErrorHelpers
- Defined in:
- lib/simple_model/base.rb
Overview
SimpleModel::Base
Provides an interface for any class to build table-less models.
Implements Validations, Callbacks and Dirty from ActiveModel, and data-type specific attribute definitions with default options. SimpleModel::Base is intended as an example, while it may be used in production, which it is on many of my apps today, it is recommend you use SimpleModel::Base as an example to implement your own model actions.
SimpleModel Actions:
Model actions provide a tool for making use of Active Model callbacks. Each action creates an instance method representing the action, which calls the method(s) listed as symbols when defining the actions. Model actions also accept a rollback option, which is called if the action fails. If you plan to implement SimpleModel’s actions, avoid naming you own methods “save”, “destroy”, “create”, and “update”, as these will override the methods defined by action.
Available Actions:
# save
# update
# create
# destroy
Example
class MyModel < SimpleModel::Base
save :my_save, :rollback => :undo_save
update :my_update, :rollback => :undo_update
destroy :my_destory, :rollback => :undo_destory
end
A basic SimpleModel implementation might resemble
class MyModel < SimpleModel::Base
has_integers :first_int, :second_int, :default => 1
has_times :now, :default => :get_now
save :save_record, :rollback => :rollback_save
def save_record
puts "saved"
true
end
def get_today
Time.now
end
def rollback_save
puts "rolled back"
end
end
Instance Attribute Summary
Attributes included from ErrorHelpers
Attributes included from Attributes
Class Method Summary collapse
- .create(*methods) ⇒ Object
-
.destroy(*methods) ⇒ Object
Destroy does not run normal validation in Rails, but with this we can if we choose to.
- .save(*methods) ⇒ Object
- .update(*methods) ⇒ Object
Methods included from ErrorHelpers
#create_error_list, #errors?, #errors_for_flash, #errors_to_s, #puralize_errors_string
Methods included from Attributes
#get, #get_attribute, #get_attribute?, #initialize, #initialized?, #set, #set_attribute
Class Method Details
.create(*methods) ⇒ Object
89 90 91 |
# File 'lib/simple_model/base.rb', line 89 def create(*methods) define_model_action(methods,:create) end |
.destroy(*methods) ⇒ Object
Destroy does not run normal validation in Rails, but with this we can if we choose to.
98 99 100 |
# File 'lib/simple_model/base.rb', line 98 def destroy(*methods) define_model_action(methods,:destroy, {:validate => false}) end |
.save(*methods) ⇒ Object
85 86 87 |
# File 'lib/simple_model/base.rb', line 85 def save(*methods) define_model_action(methods,:save) end |
.update(*methods) ⇒ Object
93 94 95 |
# File 'lib/simple_model/base.rb', line 93 def update(*methods) define_model_action(methods,:update) end |