Class: SimpleModel::Base
- Inherits:
-
Object
- Object
- SimpleModel::Base
- Includes:
- Attributes, Attributes::DefaultValueHelpers, 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
Constant Summary
Constants included from Attributes
Attributes::AVAILABLE_ATTRIBUTE_METHODS, Attributes::DEFAULT_ATTRIBUTE_SETTINGS
Instance Attribute Summary
Attributes included from ErrorHelpers
Class Method Summary collapse
-
.after_any(meth = nil) ⇒ Object
a call back that runs after a call to save, update, create or destroy even if the action fails validation (helpful for extra logging).
- .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
#attribute, #attribute_defined?, #attributes, #attributes=, #delete_attributes, #fetch_attribute_options, #get, #get_attribute, #get_attribute?, #initialize, #initialized?, #process_on_set, #raw_attribute, #reset_attributes, #set, #set_attribute, #set_raw_attribute
Class Method Details
.after_any(meth = nil) ⇒ Object
a call back that runs after a call to save, update, create or destroy even if the action fails validation (helpful for extra logging)
88 89 90 91 |
# File 'lib/simple_model/base.rb', line 88 def after_any(meth=nil) @after_any = meth if meth @after_any end |
.create(*methods) ⇒ Object
117 118 119 |
# File 'lib/simple_model/base.rb', line 117 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/simple_model/base.rb', line 96 def define_model_action(methods,action,={:validate => true}) .merge!(methods.) actions = [action,"#{action}!".to_sym] actions.each do |a| define_method(a) do |opts = {}| rslt = nil = .merge(opts) [:raise_exception] = a.to_s.match(/\!$/) run_callbacks(action) do rslt = run_model_action(methods,) end run_after_any rslt end end end |
.destroy(*methods) ⇒ Object
Destroy does not run normal validation in Rails, but with this we can if we choose to.
126 127 128 |
# File 'lib/simple_model/base.rb', line 126 def destroy(*methods) define_model_action(methods,:destroy, {:validate => false}) end |
.save(*methods) ⇒ Object
113 114 115 |
# File 'lib/simple_model/base.rb', line 113 def save(*methods) define_model_action(methods,:save) end |
.update(*methods) ⇒ Object
121 122 123 |
# File 'lib/simple_model/base.rb', line 121 def update(*methods) define_model_action(methods,:update) end |