Module: GollyUtils::Callbacks::InstanceMethods
- Defined in:
- lib/golly-utils/callbacks.rb
Overview
Provides methods that are available to instances of classes that include GollyUtils::Callbacks.
Instance Method Summary collapse
-
#run_callback(callback, options = {}) ⇒ true
Run all callbacks provided for a single callback point.
-
#run_callbacks(*callbacks, options = {}) ⇒ true
Run all callbacks provided for one or more callback points.
Instance Method Details
#run_callback(callback, options = {}) ⇒ true
Run all callbacks provided for a single callback point.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/golly-utils/callbacks.rb', line 227 def run_callback(callback, ={}) # Validate callback name name= ::GollyUtils::Callbacks.__norm_callback_key(callback) name_verified,callback_procs = self.class.send :_get_callback_procs, name raise "There is no callback defined with name #{name}." unless name_verified # Validate options = .keys - RUN_CALLBACKS_OPTIONS unless .empty? raise "Unable to recognise options: #{.map(&:inspect).sort}" end args= [:args] || [] raise "The :args option must provide an array. Invalid: #{args}" unless args.is_a?(Array) # Run callback callback_procs.each{|cb| if ctx= [:context] dlg= GollyUtils::Delegator.new self, ctx, delegate_to: :first, allow_protected: true dlg.instance_eval &cb else cb.call *args end } true end |
#run_callbacks(*callbacks, options = {}) ⇒ true
Run all callbacks provided for one or more callback points.
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/golly-utils/callbacks.rb', line 268 def run_callbacks(*callbacks) = callbacks.last.is_a?(Hash) ? callbacks.pop : {} # Run callbacks callbacks.each do |callback| run_callback callback, end true end |