Class: Vigilem::Core::Hooks::Hook
- Inherits:
-
Object
- Object
- Vigilem::Core::Hooks::Hook
- Defined in:
- lib/vigilem/core/hooks/hook.rb
Overview
bindable group of callbacks
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
Instance Method Summary collapse
- #<<(callback) ⇒ Object
- #add(&callback) ⇒ Object
-
#bind(context) ⇒ Object
The result of the evaluation.
- #call(*args, &block) ⇒ Array
- #callbacks(context = nil) ⇒ Object
- #default_body ⇒ Proc
-
#generate(klass) ⇒ Object
uses define_method instead of define_singleton_method like bind!.
- #inheritable? ⇒ TrueClass || FalseClass
-
#initialize(hook_name, options = {}, &config) ⇒ Hook
constructor
A new instance of Hook.
- #run(context, *args, &block) ⇒ Array (also: #evaluate)
-
#to_ary ⇒ Array
; [name, self].
- #to_h ⇒ name => self
- #to_proc ⇒ Proc
- #to_s ⇒ String
Constructor Details
#initialize(hook_name, options = {}, &config) ⇒ Hook
Returns a new instance of Hook.
18 19 20 21 22 23 24 |
# File 'lib/vigilem/core/hooks/hook.rb', line 18 def initialize(hook_name, ={}, &config) @name = hook_name [:inheritable] = true if [:inheritable].nil? (@options = ).freeze @callbacks = [] instance_eval(&config) if block_given? end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/vigilem/core/hooks/hook.rb', line 13 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/vigilem/core/hooks/hook.rb', line 13 def @options end |
#owner ⇒ Object
Returns the value of attribute owner.
13 14 15 |
# File 'lib/vigilem/core/hooks/hook.rb', line 13 def owner @owner end |
Instance Method Details
#<<(callback) ⇒ Object
35 36 37 |
# File 'lib/vigilem/core/hooks/hook.rb', line 35 def <<(callback) @callbacks << (callback.is_a?(Callback) ? callback : CallbackProc.new(&callback)) end |
#add(&callback) ⇒ Object
43 44 45 |
# File 'lib/vigilem/core/hooks/hook.rb', line 43 def add(&callback) self << callback end |
#bind(context) ⇒ Object
TODO:
Error for name collision
Returns the result of the evaluation.
110 111 112 113 114 115 116 117 118 |
# File 'lib/vigilem/core/hooks/hook.rb', line 110 def bind(context) hook, hook_body = self, body() self.owner = Support::Utils.get_class(context) ret = context.instance_eval do define_singleton_method(hook.name, &hook_body) end owner.hooks << hook ret end |
#call(*args, &block) ⇒ Array
58 59 60 |
# File 'lib/vigilem/core/hooks/hook.rb', line 58 def call(*args, &block) enumerate({:args => args, :block => block }, &on_run) end |
#callbacks(context = nil) ⇒ Object
50 51 52 |
# File 'lib/vigilem/core/hooks/hook.rb', line 50 def callbacks(context=nil) if context then @callbacks.reject {|cb| Utils.callback_is_in_subclass?(context, cb) } else @callbacks end end |
#default_body ⇒ Proc
76 77 78 79 80 81 82 83 84 |
# File 'lib/vigilem/core/hooks/hook.rb', line 76 def default_body @default_body ||= begin hook = self lambda do |opts={}, &block| raise 'block not given' unless block hook.callbacks << CallbackProc.new(opts, &block) end end end |
#generate(klass) ⇒ Object
uses define_method instead of define_singleton_method like bind!
hmm I could have one hook that spans multiple instances, bad or good? doesn’t work with @owner
127 128 129 130 131 132 133 134 135 |
# File 'lib/vigilem/core/hooks/hook.rb', line 127 def generate(klass) hook, hook_body = self, body() self.owner = klass ret = klass.instance_eval do define_method(hook.name, &hook_body) end klass.hooks << hook ret end |
#inheritable? ⇒ TrueClass || FalseClass
28 29 30 |
# File 'lib/vigilem/core/hooks/hook.rb', line 28 def inheritable? !!@options[:inheritable] end |
#run(context, *args, &block) ⇒ Array Also known as: evaluate
67 68 69 |
# File 'lib/vigilem/core/hooks/hook.rb', line 67 def run(context, *args, &block) enumerate({:args => args, :block => block, :context => context }, &on_run) end |
#to_ary ⇒ Array
Returns ; [name, self].
92 93 94 |
# File 'lib/vigilem/core/hooks/hook.rb', line 92 def to_ary [self.name, self] end |
#to_h ⇒ name => self
97 98 99 |
# File 'lib/vigilem/core/hooks/hook.rb', line 97 def to_h {self.name => self } end |
#to_proc ⇒ Proc
103 104 105 |
# File 'lib/vigilem/core/hooks/hook.rb', line 103 def to_proc body() end |
#to_s ⇒ String
87 88 89 |
# File 'lib/vigilem/core/hooks/hook.rb', line 87 def to_s "#{super().chomp('>')} #{name}>" end |