Class: Vigilem::Core::Hooks::ConditionalHook
- Inherits:
-
Hook
- Object
- Hook
- Vigilem::Core::Hooks::ConditionalHook
show all
- Defined in:
- lib/vigilem/core/hooks/conditional_hook.rb
Instance Attribute Summary
Attributes inherited from Hook
#name, #options, #owner
Instance Method Summary
collapse
Methods inherited from Hook
#<<, #add, #bind, #call, #callbacks, #generate, #inheritable?, #initialize, #run, #to_ary, #to_h, #to_proc, #to_s
Instance Method Details
#condition(&condition) ⇒ Object
28
29
30
|
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 28
def condition(&condition)
condition ? @condition = condition : @condition ||= lambda { true }
end
|
#default_body ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 52
def default_body
@default_body ||= begin
hook = self
lambda do |*args, &block|
hook.callbacks << CallbackProc.new({:condition_args => args }, &block)
end
end
end
|
#enumerate(args = {}, &block) ⇒ Object
enumerate over the callbacks
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 12
def enumerate(args={}, &block)
passed, failed = [], []
hook = self
super do |callback|
if hook.condition.call(*callback.options[:condition_args])
hook.passed << callback
callback.evaluate(args[:context], *args[:args], &args[:block])
else
hook.failed << callback
end
end
end
|
#failed ⇒ Array
40
41
42
|
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 40
def failed
@failed ||= []
end
|
#passed ⇒ Array
34
35
36
|
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 34
def passed
@passed ||= []
end
|
#status_hash ⇒ Array<Array,Array>
46
47
48
|
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 46
def status_hash
[[passed, failed]].hash
end
|