Module: Mixins::Hooks
- Defined in:
- lib/mixins/hooks.rb
Overview
Methods for declaring hook methods.
class MyClass
extend Mixins::Hooks
define_hook :before_fork
define_hook :after_fork
end
MyClass.before_fork do
@socket.close
end
MyClass.after_fork do
@socket = Socket.new
end
MyClass.run_before_fork_hook
fork do
MyClass.run_after_fork_hook
end
Class Method Summary collapse
-
.extended(obj) ⇒ Object
Extension callback – also extend it with MethodUtilities.
-
.make_hook_method(callbackset, **options) ⇒ Object
Create the body of a method that calls the callbacks of the given
hook. -
.make_registration_method(callbackset, **options) ⇒ Object
Create the body of a method that can register a callback for the specified
hook.
Instance Method Summary collapse
-
#define_hook(name, **options) ⇒ Object
Define a hook with the given
namethat can be registered by calling the method of the samenameand then run by calling #call_<name>_hooks.
Class Method Details
.extended(obj) ⇒ Object
Extension callback – also extend it with MethodUtilities
31 32 33 34 |
# File 'lib/mixins/hooks.rb', line 31 def self::extended( obj ) super obj.extend( Mixins::MethodUtilities ) end |
.make_hook_method(callbackset, **options) ⇒ Object
Create the body of a method that calls the callbacks of the given hook.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mixins/hooks.rb', line 50 def self::make_hook_method( callbackset, ** ) return lambda do |*args| set = self.public_send( callbackset ) or raise "No hook callback registration set!" self.public_send( "#{callbackset}_run=", true ) set.to_a.each do |callback| callback.call( *args ) end end end |
.make_registration_method(callbackset, **options) ⇒ Object
Create the body of a method that can register a callback for the specified hook.
38 39 40 41 42 43 44 45 46 |
# File 'lib/mixins/hooks.rb', line 38 def self::make_registration_method( callbackset, ** ) return lambda do |&callback| raise LocalJumpError, "no callback given" unless callback set = self.public_send( callbackset ) or raise "No hook registration set!" set.add( callback ) callback.call if self.public_send( "#{callbackset}_run?" ) end end |
Instance Method Details
#define_hook(name, **options) ⇒ Object
Define a hook with the given name that can be registered by calling the method of the same name and then run by calling #call_<name>_hooks.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mixins/hooks.rb', line 65 def define_hook( name, ** ) callbacks_name = "#{name}_callbacks" self.instance_variable_set( "@#{callbacks_name}", Set.new ) self.singleton_attr_reader( callbacks_name ) self.instance_variable_set( "@#{callbacks_name}_run", false ) self.singleton_predicate_accessor( "#{callbacks_name}_run" ) register_body = Mixins::Hooks. make_registration_method( callbacks_name, ** ) self.singleton_class.define_method( name, ®ister_body ) calling_body = Mixins::Hooks. make_hook_method( callbacks_name, ** ) self.singleton_class.define_method( "call_#{name}_hook", &calling_body ) self.singleton_method_alias( "run_#{name}_hook", "call_#{name}_hook" ) end |