Sha256: ad7cdb4168b4bc8c99bb68d287348ccfe59d0654145c1de88718784d33d12708

Contents?: true

Size: 846 Bytes

Versions: 4

Compression:

Stored size: 846 Bytes

Contents

module Restspec::Schema::Types
  class ArrayType < BasicType
    def example_for(attribute)
      length_only_works_with_parameterized_types!

      example_length.times.map do
        parameterized_type.example_for(attribute)
      end
    end

    def valid?(attribute, value)
      is_array = value.is_a?(Array)
      if parameterized_type
        is_array && value.all? do |item|
          parameterized_type.totally_valid?(attribute, item)
        end
      else
        is_array
      end
    end

    private

    def example_length
      example_options.fetch(:length, 0)
    end

    def length_only_works_with_parameterized_types!
      if example_options.has_key?(:length) && !parameterized_type
        raise "To use the :length option you need to have a parameterized_type or we can't generate the array"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
restspec-0.0.4 lib/restspec/schema/types/array_type.rb
restspec-0.0.3 lib/restspec/schema/types/array_type.rb
restspec-0.0.2 lib/restspec/schema/types/array_type.rb
restspec-0.0.1 lib/restspec/schema/types/array_type.rb