Class: Vigilem::Core::Hooks::ConditionalHook

Inherits:
Hook
  • Object
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

Constructor Details

This class inherits a constructor from Vigilem::Core::Hooks::Hook

Instance Method Details

#condition(&condition) ⇒ Object

Parameters:

  • condition (Proc)

Returns:



28
29
30
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 28

def condition(&condition)
  condition ? @condition = condition : @condition ||= lambda { true }
end

#default_bodyObject

Returns:



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

Parameters:

  • args (Array) (defaults to: {})
  • block (Proc)

Returns:



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

#failedArray

Returns:

  • (Array)


40
41
42
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 40

def failed
  @failed ||= []
end

#passedArray

Returns:

  • (Array)


34
35
36
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 34

def passed
  @passed ||= []
end

#status_hashArray<Array,Array>

Returns:

  • (Array<Array,Array>)


46
47
48
# File 'lib/vigilem/core/hooks/conditional_hook.rb', line 46

def status_hash
  [[passed, failed]].hash
end