Sha256: c5142a3857145f46c0ea3399ce41ace7c23739cb676f9db2c03d2f650578c6a4

Contents?: true

Size: 692 Bytes

Versions: 3

Compression:

Stored size: 692 Bytes

Contents

module Datacaster
  class Caster < Base
    def initialize(&block)
      raise "Expected block" unless block_given?

      @cast = block
    end

    def cast(object, runtime:)
      result = Runtimes::Base.(runtime, @cast, object)

      raise TypeError.new("Either Datacaster::Result or Dry::Monads::Result " \
        "should be returned from cast block") unless [Datacaster::Result, Dry::Monads::Result].any? { |k| result.is_a?(k) }

      if result.is_a?(Dry::Monads::Result)
        result = result.success? ? Datacaster.ValidResult(result.value!) : Datacaster.ErrorResult(result.failure)
      end

      result
    end

    def inspect
      "#<Datacaster::Caster>"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
datacaster-3.1.1 lib/datacaster/caster.rb
datacaster-3.1.0 lib/datacaster/caster.rb
datacaster-3.0.0 lib/datacaster/caster.rb