class Restspec::Schema::Types::ArrayType
Public Instance Methods
example_for(attribute)
click to toggle source
Generates an example array.
@example without a parameterized type
# schema attribute :name, array # examples example_for(schema.attributes[:name]) # => []
@example with a parameterized type and no length example option
# schema attribute :name, array.of(string) # examples example_for(schema.attributes[:name]) # => ['hola', 'mundo'] # the length is something randomly between 1 a 5.
@example with a parameterized type and length example option
# schema attribute :name, array(length: 2).of(string) # or: attribute :name, array(example_options: { length: 2}).of(string) # examples example_for(schema.attributes[:name]) # => ['hola', 'mundo'] # the length will always be 2
@param attribute [Restspec::Schema::Attribute] the atribute of the schema. @return [Array] Generated array for examples.
# File lib/restspec/schema/types/array_type.rb, line 29 def example_for(attribute) length_only_works_with_parameterized_types! example_length.times.map do parameterized_type.example_for(attribute) end end
valid?(attribute, value)
click to toggle source
Validates if the array is valid.
-
Without a parameterized type, it only checks if the value is an array.
-
With a parameterized type, it checks is every object inside the array is valid against the parameterized type.
@param attribute [Restspec::Schema::Attribute] the atribute of the schema. @param value [Object] the value of the attribute.
@return [true, false] If the array is valid.
# File lib/restspec/schema/types/array_type.rb, line 47 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 Instance Methods
example_length()
click to toggle source
# File lib/restspec/schema/types/array_type.rb, line 60 def example_length example_options.fetch(:length, internal_length) end
internal_length()
click to toggle source
# File lib/restspec/schema/types/array_type.rb, line 64 def internal_length return 0 if !parameterized_type rand(1..5) end
length_only_works_with_parameterized_types!()
click to toggle source
# File lib/restspec/schema/types/array_type.rb, line 69 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