Sha256: 5cdf08516edfbed69c7ba864f98e2ea8bc49ad28cc2e324c5eeb6443a6885bd8

Contents?: true

Size: 1009 Bytes

Versions: 7

Compression:

Stored size: 1009 Bytes

Contents

module Rasti
  class Form
    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 ValidationError => error
              error.errors.each do |k,v|
                errors["#{index}.#{k}"] = v
              end
            rescue => error
              errors[index] = [error.message]
            end
          end

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

          result
        end

      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rasti-form-3.1.2 lib/rasti/form/types/array.rb
rasti-form-3.1.1 lib/rasti/form/types/array.rb
rasti-form-3.1.0 lib/rasti/form/types/array.rb
rasti-form-3.0.0 lib/rasti/form/types/array.rb
rasti-form-2.2.0 lib/rasti/form/types/array.rb
rasti-form-2.1.0 lib/rasti/form/types/array.rb
rasti-form-2.0.0 lib/rasti/form/types/array.rb