Sha256: f8bc92d75e3e5748148fe9010d4af56ca92769680a21ed1f0e3d562ee75d2896

Contents?: true

Size: 921 Bytes

Versions: 4

Compression:

Stored size: 921 Bytes

Contents

module Rasti
  module Types
    class Array

      include Castable

      attr_reader :type

      def self.[](type)
        new type
      end

      def to_s
        "#{self.class}[#{type}]"
      end
      alias_method :inspect, :to_s

      private

      def initialize(type)
        @type = type
      end

      def valid?(value)
        value.is_a? ::Array
      end

      def transform(value)
        result = []
        errors = {}

        value.each_with_index do |e,i|
          index = i + 1
          begin
            result << type.cast(e)

          rescue CompoundError => ex
            ex.errors.each do |key, messages|
              errors["#{index}.#{key}"] = messages
            end

          rescue => ex
            errors[index] = [ex.message]
          end
        end

        raise MultiCastError.new(self, value, errors) unless errors.empty?

        result
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rasti-types-1.1.2 lib/rasti/types/array.rb
rasti-types-1.1.1 lib/rasti/types/array.rb
rasti-types-1.1.0 lib/rasti/types/array.rb
rasti-types-1.0.0 lib/rasti/types/array.rb