class Pathway::Plugins::Responder::Responder

Public Class Methods

new(result, &bl) click to toggle source
# File lib/pathway/plugins/responder.rb, line 19
def initialize(result, &bl)
  @result, @context, @fails = result, bl.binding.receiver, {}
  instance_eval(&bl)
end
respond(result, &bl) click to toggle source
# File lib/pathway/plugins/responder.rb, line 14
def self.respond(result, &bl)
  r = new(result, &bl)
  r.respond
end

Public Instance Methods

failure(type = nil, &bl) click to toggle source
# File lib/pathway/plugins/responder.rb, line 28
def failure(type = nil, &bl)
  if type.nil?
    @fail_default = bl
  else
    @fails[type] = bl
  end
end
respond() click to toggle source
# File lib/pathway/plugins/responder.rb, line 36
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) click to toggle source
# File lib/pathway/plugins/responder.rb, line 24
def success(&bl)
  @ok = bl
end