Sha256: 18760f0bfd1d10caba1d3a08d3c0a4c0714bfa59d9979e4e0aaed2e3930dbe8e

Contents?: true

Size: 687 Bytes

Versions: 3

Compression:

Stored size: 687 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)

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

      raise TypeError.new("Either Datacaster::Result or Dry::Monads::Result " \
        "should be returned from cast block") unless result.is_a?(Datacaster::Result)

      result
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
datacaster-3.1.5 lib/datacaster/caster.rb
datacaster-3.1.3 lib/datacaster/caster.rb
datacaster-3.1.2 lib/datacaster/caster.rb