Module: PlainRecord::Callbacks
- Included in:
- Model
- Defined in:
- lib/plain_record/callbacks.rb
Overview
Callbacks are hooks that allow you to define methods to run before and after some method, to change it logic.
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
Hash of class callbacks with field.
Instance Method Summary collapse
-
#after(events, priority = 1, &block) ⇒ Object
Set block as callback after
events
. -
#before(events, priority = 1, &block) ⇒ Object
Set block as callback before
events
. -
#call_after_callbacks(event, result, params) ⇒ Object
Call
before
callbacks forevent
withparams
. -
#call_before_callbacks(event, params) ⇒ Object
Call
before
callbacks forevent
withparams
. -
#use_callbacks(event, *params, &block) ⇒ Object
Call before callback for
event
, run block and give it result to after callbacks.
Instance Attribute Details
#callbacks ⇒ Object
Hash of class callbacks with field.
26 27 28 |
# File 'lib/plain_record/callbacks.rb', line 26 def callbacks @callbacks end |
Instance Method Details
#after(events, priority = 1, &block) ⇒ Object
Set block as callback after events
. Callback with less priority
will start earlier.
After callbacks may change method return, which will be pass as first argument for first callback. It return will be pass for next callback and so on.
class Person
include PlainRecord::Callbacks
def name
use_callbacks(:name) do
'John'
end
end
end
class GreatPerson < Person
after :name, 2 do |name|
'Great ' + name
end
after :name do |name|
'The ' + name
end
end
GreatPerson.new.name #=> "The Great John"
89 90 91 92 93 |
# File 'lib/plain_record/callbacks.rb', line 89 def after(events, priority = 1, &block) Array(events).each do |event| add_callback(:after, event, priority, block) end end |
#before(events, priority = 1, &block) ⇒ Object
Set block as callback before events
. Callback with less priority
will start earlier.
class File
include PlainRecord::Callbacks
attr_accessor :name
attr_accessor :content
def save
use_callbacks(:save, self) do
File.open(@name, 'w') { |io| io.puts @content }
end
end
end
class NewFile < File
before :save do |file|
while File.exists? file.name
file.name = 'another ' + file.name
end
end
before, :save do
raise ArgumentError if 255 < @name.length
end
end
55 56 57 58 59 |
# File 'lib/plain_record/callbacks.rb', line 55 def before(events, priority = 1, &block) Array(events).each do |event| add_callback(:before, event, priority, block) end end |
#call_after_callbacks(event, result, params) ⇒ Object
Call before
callbacks for event
with params
. Callbacks can change result
. In your code use more pretty use_callbacks
method.
106 107 108 109 110 111 112 |
# File 'lib/plain_record/callbacks.rb', line 106 def call_after_callbacks(event, result, params) init_callbacks(event) @callbacks[:after][event].each do |after, priority| result = after.call(result, *params) end result end |
#call_before_callbacks(event, params) ⇒ Object
Call before
callbacks for event
with params
. In your code use more pretty use_callbacks
method.
97 98 99 100 101 102 |
# File 'lib/plain_record/callbacks.rb', line 97 def call_before_callbacks(event, params) init_callbacks(event) @callbacks[:before][event].each do |before, priority| before.call(*params) end end |
#use_callbacks(event, *params, &block) ⇒ Object
Call before callback for event
, run block and give it result to after callbacks.
def my_save_method(entry)
use_callbacks(:save, enrty) do
entry.file.write
end
end
122 123 124 125 126 |
# File 'lib/plain_record/callbacks.rb', line 122 def use_callbacks(event, *params, &block) call_before_callbacks(event, params) result = yield call_after_callbacks(event, result, params) end |