Sha256: 43d6d2383ffb220cd2e8904a5625ffca239a0b0497b6f4088a72e3094af99dc4

Contents?: true

Size: 764 Bytes

Versions: 19

Compression:

Stored size: 764 Bytes

Contents

module Pathway
  class Responder
    def self.respond(result, &bl)
      r = new(result, &bl)
      r.respond
    end

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

    def success(&bl)
      @ok = bl
    end

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

    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
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
pathway-0.7.0 lib/pathway/responder.rb
pathway-0.6.2 lib/pathway/responder.rb
pathway-0.6.1 lib/pathway/responder.rb
pathway-0.6.0 lib/pathway/responder.rb
pathway-0.5.1 lib/pathway/responder.rb
pathway-0.5.0 lib/pathway/responder.rb
pathway-0.4.0 lib/pathway/responder.rb
pathway-0.0.20 lib/pathway/responder.rb
pathway-0.0.19 lib/pathway/responder.rb
pathway-0.0.18 lib/pathway/responder.rb
pathway-0.0.17 lib/pathway/responder.rb
pathway-0.0.16 lib/pathway/responder.rb
pathway-0.0.15 lib/pathway/responder.rb
pathway-0.0.14 lib/pathway/responder.rb
pathway-0.0.13 lib/pathway/responder.rb
pathway-0.0.12 lib/pathway/responder.rb
pathway-0.0.11 lib/pathway/responder.rb
pathway-0.0.10 lib/pathway/responder.rb
pathway-0.0.9 lib/pathway/responder.rb