Class: Flor::Hook

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

Overview

Used by Flor::Loader to prepare hooks from hooks.json files.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit, exid, h) ⇒ Hook

Returns a new instance of Hook.



9
10
11
12
13
14
# File 'lib/flor/unit/hook.rb', line 9

def initialize(unit, exid, h)

  @unit = unit
  @exid = exid
  @h = h
end

Class Method Details

.instantiate(unit, hook_class) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/flor/unit/hook.rb', line 105

def instantiate(unit, hook_class)

  c =
    case hook_class
    when String then Flor.const_get(hook_class)
    else hook_class
    end
  a =
    case i = c.instance_method(:initialize).arity
    when 0, 1 then [ unit ][0, i]
    else []
    end

  c.new(*a)
end

Instance Method Details

#class_to_hookObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/flor/unit/hook.rb', line 68

def class_to_hook

  i = Flor::Hook.instantiate(@unit, @h['class'])

  h = @h.dup
  h.merge!(Flor.to_string_keyed_hash(i.opts)) if i.respond_to?(:opts)
  opts = extract_filters(h)

  [ "hooc#{object_id}", opts, i, nil ]
end

#classical_to_hookObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/flor/unit/hook.rb', line 90

def classical_to_hook

  opts = extract_filters(@h)

  #correct_points(opts)
    #
    # Necessary since "cancel" gets interpreted as
    # [ '_proc', { 'proc' => 'cancel' }, @line ]
    # ...

  [ "hook#{object_id}", opts, self, nil ]
end

#extract_filters(h) ⇒ Object

protected

def correct_points(opts)

  pts = opts[:point]; return unless pts

  opts[:point] =
    Flor.is_tree?(pts) ?
    correct_point(pts) :
    pts.collect { |pt| correct_point(pt) }
end

def correct_point(point)

  return point[1]['proc'] if point.is_a?(Array) && point[0] == '_proc'
  point
end


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/flor/unit/hook.rb', line 55

def extract_filters(h)

  r = {}
  r[:consumed] = h['consumed']
  r[:point] = Flor.h_fetch_a(h, 'points', 'point', nil)
  r[:nid] = Flor.h_fetch_a(h, 'nids', 'nid', nil)
  r[:heap] = Flor.h_fetch_a(h, 'heaps', 'heap', nil)
  r[:heat] = Flor.h_fetch_a(h, 'heats', 'heat', nil)
  #opts[:name] = data['names']

  r
end

#instance_to_hookObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/flor/unit/hook.rb', line 79

def instance_to_hook

  i = @h['instance']

  h = @h.dup
  h.merge!(Flor.to_string_keyed_hash(i.opts)) if i.respond_to?(:opts)
  opts = extract_filters(h)

  [ "hooi#{object_id}", opts, i, nil ]
end

#notify(executor, message) ⇒ Object



29
30
31
32
# File 'lib/flor/unit/hook.rb', line 29

def notify(executor, message)

  @unit.caller.call(executor, @h, message)
end

#to_hookObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flor/unit/hook.rb', line 16

def to_hook

  @unit.loader.require(@h)

  if @h['class']
    class_to_hook
  elsif @h['instance']
    instance_to_hook
  else
    classical_to_hook
  end
end