Sha256: 0038151a8aa0473666bd0bb68afab99a54969b1f405c42fcd13cd974c605fed1

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module BloodContracts
  module Core
    class Tuple < Refined
      class << self
        attr_reader :attributes, :names, :finalized

        def new(*args, **kwargs)
          return super(*args, **kwargs) if finalized

          raise ArgumentError unless args.all?(Class)
          pipe = Class.new(Tuple) { def inspect; super; end }
          pipe.instance_variable_set(:@attributes, args)
          pipe.instance_variable_set(:@names, kwargs[:names].to_a)
          pipe.instance_variable_set(:@finalized, true)
          pipe
        end
      end

      attr_reader :values
      def initialize(*args)
        super
        @values = args
      end

      def match
        super do
          matches = self.class.attributes.zip(values).map do |(type, value)|
            type.match(value, context: @context)
          end
          next self unless (failure = matches.find?(&:invalid?)).nil?
          failure
        end
      end

      def unpack
        super { |match| match.values.map(&method(:unpack_refined)) }
      end
      alias :to_ary :unpack

      private def values_by_names
        if self.class.names.empty?
          self.values
        else
          self.class.names.zip(attributes).map { |k, v| [k, v].join('=') }
        end
      end

      private def inspect
        "#<tuple #{self.class.name} (#{values_by_names.join(',')}>"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blood_contracts-core-0.2.0 lib/blood_contracts/core/tuple.rb