Sha256: a67b3052ed02821f233ea740d0f5eb0092369643f42463e9ff4f04903508b574

Contents?: true

Size: 718 Bytes

Versions: 12

Compression:

Stored size: 718 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, instead got #{result.inspect}") unless result.is_a?(Datacaster::Result)

      result
    end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
datacaster-4.1.0 lib/datacaster/caster.rb
datacaster-4.0.1 lib/datacaster/caster.rb
datacaster-3.3.1 lib/datacaster/caster.rb
datacaster-3.3.0 lib/datacaster/caster.rb
datacaster-3.2.7 lib/datacaster/caster.rb
datacaster-3.2.6 lib/datacaster/caster.rb
datacaster-3.2.5 lib/datacaster/caster.rb
datacaster-3.2.4 lib/datacaster/caster.rb
datacaster-3.2.3 lib/datacaster/caster.rb
datacaster-3.2.2 lib/datacaster/caster.rb
datacaster-3.2.1 lib/datacaster/caster.rb
datacaster-3.2.0 lib/datacaster/caster.rb