Sha256: 63ca03f1e9ca01dfca7fc745f1cc16c2a6b1cfbc60c1fabf0bc9ec73db5d1d66

Contents?: true

Size: 1014 Bytes

Versions: 5

Compression:

Stored size: 1014 Bytes

Contents

# frozen_string_literal: true

require 'plumb/composable'

module Plumb
  class TupleClass
    include Composable

    attr_reader :children

    def initialize(*children)
      @children = children.map { |t| Composable.wrap(t) }.freeze
      freeze
    end

    def of(*types)
      self.class.new(*types)
    end

    alias [] of

    def call(result)
      return result.invalid(errors: 'must be an Array') unless result.value.is_a?(::Array)
      return result.invalid(errors: 'must have the same size') unless result.value.size == @children.size

      errors = {}
      values = @children.map.with_index do |type, idx|
        val = result.value[idx]
        r = type.resolve(val)
        errors[idx] = ["expected #{type.inspect}, got #{val.inspect}", r.errors].flatten unless r.valid?
        r.value
      end

      return result.valid(values) unless errors.any?

      result.invalid(errors:)
    end

    private

    def _inspect
      "Tuple[#{@children.map(&:inspect).join(', ')}]"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
plumb-0.0.8 lib/plumb/tuple_class.rb
plumb-0.0.7 lib/plumb/tuple_class.rb
plumb-0.0.6 lib/plumb/tuple_class.rb
plumb-0.0.5 lib/plumb/tuple_class.rb
plumb-0.0.4 lib/plumb/tuple_class.rb