Class: Switchblock::BlockSwitcher

Inherits:
BlankSlate
  • Object
show all
Defined in:
lib/switchblock.rb

Instance Method Summary collapse

Constructor Details

#initialize(called_block, args) ⇒ BlockSwitcher

Returns a new instance of BlockSwitcher.



10
11
12
13
14
15
16
# File 'lib/switchblock.rb', line 10

def initialize(called_block, args)
  @called_block = called_block
  @args = args
  @ret = nil
  @called = false
  @callees = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/switchblock.rb', line 22

def method_missing(method,*args, &block)
  if (method.downcase == @called_block.to_sym.downcase or method == :always) or (@called == false and method == :else) then
    @called = true
    if args.size > 0 then
      @ret = args.first.call(*@args)
    else
      @ret = block.call(*@args)
    end
  end
  # For error reporting
  @callees << method
  @callees.uniq!
  # Always return something meaningful
  @ret
end

Instance Method Details

#ensureObject



18
19
20
# File 'lib/switchblock.rb', line 18

def ensure
  raise NothingExecutedError, "Nothing got executed! Expected one of #{@callees.inspect}, but only got #{@called_block}" if not @called
end