Class: Flor::Hooker

Inherits:
Object
  • Object
show all
Defined in:
lib/flor/unit/hooker.rb

Constant Summary collapse

KEYS =
{
  consumed:   [:consumed, :c].freeze,
  point:      [:point, :p].freeze,
  nid:        [:nid].freeze,
  exid:       [:exid].freeze,
  domain:     [:domain, :d].freeze,
  subdomain:  [:subdomain, :sd].freeze,
  tag:        [:tag, :t].freeze,
  name:       [:name, :n].freeze,
  subnid:     [:subnid].freeze,
  heap:       [:heap, :hp].freeze,
  heat:       [:heat, :ht].freeze
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit) ⇒ Hooker

Returns a new instance of Hooker.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/flor/unit/hooker.rb', line 25

def initialize(unit)

  @unit = unit

  class << unit

    def hook(*args, &block)

      @hooker.add(*args, &block)
    end
  end

  @hooks = []
end

Instance Attribute Details

#hooksObject (readonly)

NB: logger configuration entries start with “hok_”



9
10
11
# File 'lib/flor/unit/hooker.rb', line 9

def hooks
  @hooks
end

Instance Method Details

#[](name) ⇒ Object



48
49
50
51
52
53
# File 'lib/flor/unit/hooker.rb', line 48

def [](name)

  h = @hooks.find { |n, _, _, _| n == name }

  h ? h[2] || h[3] : nil
end

#add(*args, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/flor/unit/hooker.rb', line 60

def add(*args, &block)

  name = nil
  hook = nil
  opts = {}

  args.each do |arg|
    case arg
    when String then name = arg
    when Hash then opts = arg
    else hook = arg
    end
  end

  hook = Flor::Hook.instantiate(@unit, hook) \
    if hook.is_a?(Class)

  name ||= hook.name \
    if hook.respond_to?(:name) && hook.method(:name).arity < 1

  @hooks << [ name, opts, hook, block ]
end

#notify(executor, message) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/flor/unit/hooker.rb', line 83

def notify(executor, message)

  (
    @hooks + executor.traps_and_hooks
  )
    .inject([]) { |a, (_, opts, hook, block)|
      # name of hook is piped into "_" oblivion

      a.concat(
        if ! match?(executor, hook, opts, message)
          []
        elsif hook.is_a?(Flor::Trap)
          executor.trigger_trap(hook, message)
        elsif hook
          executor.trigger_hook(hook, message)
        else
          executor.trigger_block(block, opts, message)
        end) }
end

#shutdownObject



40
41
42
43
44
45
46
# File 'lib/flor/unit/hooker.rb', line 40

def shutdown

  @hooks.each do |n, o, hook, b|

    hook.shutdown if hook.respond_to?(:shutdown)
  end
end

#wlistObject



55
56
57
58
# File 'lib/flor/unit/hooker.rb', line 55

def wlist

  @wlist ||= self['wlist']
end