Module: Ducktape::Hookable
- Included in:
- Array, BindableAttribute, Hash, String
- Defined in:
- lib/ducktape/hookable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #add_hook(event, hook = nil, &block) ⇒ Object
- #clear_hooks(event = nil) ⇒ Object
- #remove_hook(event, hook) ⇒ Object
Class Method Details
.extended(_) ⇒ Object
48 49 50 |
# File 'lib/ducktape/hookable.rb', line 48 def self.extended(_) raise 'Cannot extend, only include.' end |
.included(base) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ducktape/hookable.rb', line 37 def self.included(base) base.extend(ClassMethods) base.def_hook :on_changed unless base.method_defined?(:on_changed) return unless base.is_a?(Module) included = base.respond_to?(:included) && base.method(:included) base.define_singleton_method(:included, ->(c) do included.(c) if included c.extend(ClassMethods) end) end |
Instance Method Details
#add_hook(event, hook = nil, &block) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/ducktape/hookable.rb', line 52 def add_hook(event, hook = nil, &block) hook = block if block #block has precedence raise ArgumentError, 'no hook was passed' unless hook hook = hook.to_s unless hook.respond_to?(:call) self.hooks[event.to_s].unshift(hook) hook end |
#clear_hooks(event = nil) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/ducktape/hookable.rb', line 65 def clear_hooks(event = nil) if event self.hooks.delete(event.to_s) else self.hooks.clear.dup end end |
#remove_hook(event, hook) ⇒ Object
60 61 62 63 |
# File 'lib/ducktape/hookable.rb', line 60 def remove_hook(event, hook) hook = hook.to_s unless hook.respond_to?(:call) self.hooks[event.to_s].delete(hook) end |