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
-
.define_model_action(methods, action, default_options = {:validate => true}) ⇒ Object
Defines the model action’s instance methods and applied defaults.
-
.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
104 105 106 |
# File 'lib/simple_model/base.rb', line 104 def create(*methods) define_model_action(methods,:create) end |
.define_model_action(methods, action, default_options = {:validate => true}) ⇒ Object
Defines the model action’s instance methods and applied defaults. For every action defined, we also define that actions ! method which raises exceptions when the action fails.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/simple_model/base.rb', line 86 def define_model_action(methods,action,={:validate => true}) .merge!(methods.) actions = [action,"#{action}!".to_sym] actions.each do |a| define_method(a) do |opts = {}| = .merge(opts) [:raise_exception] = a.to_s.match(/\!$/) run_callbacks(action) do run_model_action(methods,) end end end end |
.destroy(*methods) ⇒ Object
Destroy does not run normal validation in Rails, but with this we can if we choose to.
113 114 115 |
# File 'lib/simple_model/base.rb', line 113 def destroy(*methods) define_model_action(methods,:destroy, {:validate => false}) end |
.save(*methods) ⇒ Object
100 101 102 |
# File 'lib/simple_model/base.rb', line 100 def save(*methods) define_model_action(methods,:save) end |
.update(*methods) ⇒ Object
108 109 110 |
# File 'lib/simple_model/base.rb', line 108 def update(*methods) define_model_action(methods,:update) end |