Sha256: 796b36c5db256eb398e8fc091bba55529f2f6841a414f7a161da35c633c1d8a8

Contents?: true

Size: 876 Bytes

Versions: 17

Compression:

Stored size: 876 Bytes

Contents

module Datacaster
  class ThenNode < Base
    def initialize(left, then_caster, else_caster = nil)
      @left = left
      @then = then_caster
      @else = else_caster
    end

    def else(else_caster)
      raise ArgumentError.new("Datacaster: double else clause is not permitted") if @else

      self.class.new(@left, @then, DefinitionDSL.expand(else_caster))
    end

    def cast(object, runtime:)
      unless @else
        raise ArgumentError.new('Datacaster: use "a & b" instead of "a.then(b)" when there is no else-clause')
      end

      left_result = @left.with_runtime(runtime).(object)

      if left_result.valid?
        @then.with_runtime(runtime).(left_result.value)
      else
        @else.with_runtime(runtime).(object)
      end
    end

    def inspect
      "#<Datacaster::ThenNode Then: #{@then.inspect} Else: #{@else.inspect}>"
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
datacaster-4.1.0 lib/datacaster/then_node.rb
datacaster-4.0.1 lib/datacaster/then_node.rb
datacaster-3.3.1 lib/datacaster/then_node.rb
datacaster-3.3.0 lib/datacaster/then_node.rb
datacaster-3.2.7 lib/datacaster/then_node.rb
datacaster-3.2.6 lib/datacaster/then_node.rb
datacaster-3.2.5 lib/datacaster/then_node.rb
datacaster-3.2.4 lib/datacaster/then_node.rb
datacaster-3.2.3 lib/datacaster/then_node.rb
datacaster-3.2.2 lib/datacaster/then_node.rb
datacaster-3.2.1 lib/datacaster/then_node.rb
datacaster-3.2.0 lib/datacaster/then_node.rb
datacaster-3.1.5 lib/datacaster/then_node.rb
datacaster-3.1.3 lib/datacaster/then_node.rb
datacaster-3.1.2 lib/datacaster/then_node.rb
datacaster-3.1.1 lib/datacaster/then_node.rb
datacaster-3.1.0 lib/datacaster/then_node.rb