Class: Dynflow::Middleware::Register

Inherits:
Object
  • Object
show all
Includes:
Algebrick::TypeCheck
Defined in:
lib/dynflow/middleware/register.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegister

Returns a new instance of Register.



7
8
9
10
11
12
13
# File 'lib/dynflow/middleware/register.rb', line 7

def initialize
  @rules = Hash.new do |h, k|
    h[k] = { before:  [],
             after:   [],
             replace: [] }
  end
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



5
6
7
# File 'lib/dynflow/middleware/register.rb', line 5

def rules
  @rules
end

Instance Method Details

#merge!(register) ⇒ Object



25
26
27
28
29
30
# File 'lib/dynflow/middleware/register.rb', line 25

def merge!(register)
  Type! register, Middleware::Register
  register.rules.each do |klass, rules|
    use(klass, rules)
  end
end

#use(middleware_class, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dynflow/middleware/register.rb', line 15

def use(middleware_class, options = {})
  unknown_options = options.keys - [:before, :after, :replace]
  if unknown_options.any?
    raise ArgumentError, "Unexpected options: #{unknown_options}"
  end
  @rules[middleware_class].merge!(options) do |key, old, new|
    old + Array(new)
  end
end