Class: SimpleModel::Base

Inherits:
Object
  • Object
show all
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

#errors_count

Class Method Summary collapse

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



86
87
88
89
# File 'lib/simple_model/base.rb', line 86

def after_any(meth=nil)
  @after_any = meth if meth
  @after_any
end

.create(*methods) ⇒ Object



115
116
117
# File 'lib/simple_model/base.rb', line 115

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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/simple_model/base.rb', line 94

def define_model_action(methods,action,default_options={:validate => true})
  default_options.merge!(methods.extract_options!)
  actions = [action,"#{action}!".to_sym]
  actions.each do |a|
    define_method(a) do |opts = {}|
      rslt = nil
      options = default_options.merge(opts)
      options[:raise_exception] = a.to_s.match(/\!$/)
      run_callbacks(action) do
        rslt = run_model_action(methods,options)
      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.



124
125
126
# File 'lib/simple_model/base.rb', line 124

def destroy(*methods)
  define_model_action(methods,:destroy, {:validate => false})
end

.save(*methods) ⇒ Object



111
112
113
# File 'lib/simple_model/base.rb', line 111

def save(*methods)
  define_model_action(methods,:save)
end

.update(*methods) ⇒ Object



119
120
121
# File 'lib/simple_model/base.rb', line 119

def update(*methods)
  define_model_action(methods,:update)
end