Class: Pathway::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/pathway/responder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, &bl) ⇒ Responder

Returns a new instance of Responder.



8
9
10
11
# File 'lib/pathway/responder.rb', line 8

def initialize(result, &bl)
  @result, @context, @fails = result, bl.binding.receiver, {}
  instance_eval(&bl)
end

Class Method Details

.respond(result, &bl) ⇒ Object



3
4
5
6
# File 'lib/pathway/responder.rb', line 3

def self.respond(result, &bl)
  r = new(result, &bl)
  r.respond
end

Instance Method Details

#failure(type = nil, &bl) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/pathway/responder.rb', line 17

def failure(type = nil, &bl)
  if type.nil?
    @fail_default = bl
  else
    @fails[type] = bl
  end
end

#respondObject



25
26
27
28
29
30
31
32
33
# File 'lib/pathway/responder.rb', line 25

def respond
  if @result.success?
    @context.instance_exec(@result.value, &@ok)
  elsif Error === @result.error && fail_block = @fails[@result.error.type]
    @context.instance_exec(@result.error, &fail_block)
  else
    @context.instance_exec(@result.error, &@fail_default)
  end
end

#success(&bl) ⇒ Object



13
14
15
# File 'lib/pathway/responder.rb', line 13

def success(&bl)
  @ok = bl
end