-
Notifications
You must be signed in to change notification settings - Fork 3
Callback Results
mosop edited this page Feb 2, 2017
·
19 revisions
The run_callbacks_for_* method returns a value that a given block returns.
class Smile
Callback.enable
define_callback_group :smile
def smile
run_callbacks_for_smile do
":)"
end
end
end
Record.new.smile # => ":)"
run_callbacks_for_* also stores all values returned from callbacks. You can access the returned values by the first argument of a block passed to run_callbacks_for_* or the callback_results_for_* method. You can name a callback for getting its result by the name.
class Record
Callback.enable
define_callback_group :validate, proc_type: Proc(Bool)
getter name : String
getter email : String?
def initialize(@name, @email = nil)
end
on_validate :mosop? do |o|
o.name == "mosop"
end
on_validate do |o|
!o.email.nil?
end
def validate
run_callbacks_for_validate do |results|
results.values.all?
end
end
end
rec = Record.new(name: "mosop")
rec.validate # => false
results = rec.callback_results_for_validate
results.values # => [true, false]
results[:mosop?] # => true