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
- .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
.create(*methods) ⇒ Object
107 108 109 |
# File 'lib/simple_model/base.rb', line 107 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/simple_model/base.rb', line 89 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.
116 117 118 |
# File 'lib/simple_model/base.rb', line 116 def destroy(*methods) define_model_action(methods,:destroy, {:validate => false}) end |
.save(*methods) ⇒ Object
103 104 105 |
# File 'lib/simple_model/base.rb', line 103 def save(*methods) define_model_action(methods,:save) end |
.update(*methods) ⇒ Object
111 112 113 |
# File 'lib/simple_model/base.rb', line 111 def update(*methods) define_model_action(methods,:update) end |