Class: T::Types::FixedArray
Overview
Takes a list of types. Validates each item in an array using the type in the same position in the list.
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#describe_obj(obj) ⇒ Object
This gives us better errors, e.g.: “Expected [String, Symbol], got [String, String]” instead of “Expected [String, Symbol], got Array”.
-
#initialize(types) ⇒ FixedArray
constructor
A new instance of FixedArray.
- #name ⇒ Object
- #valid?(obj) ⇒ Boolean
Methods inherited from Base
#==, #error_message_for_obj, #hash, method_added, #subtype_of?, #to_s, #validate!
Constructor Details
#initialize(types) ⇒ FixedArray
Returns a new instance of FixedArray.
11 12 13 |
# File 'lib/types/types/fixed_array.rb', line 11 def initialize(types) @types = types.map {|type| T::Utils.coerce(type)} end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
9 10 11 |
# File 'lib/types/types/fixed_array.rb', line 9 def types @types end |
Instance Method Details
#describe_obj(obj) ⇒ Object
This gives us better errors, e.g.: “Expected [String, Symbol], got [String, String]” instead of “Expected [String, Symbol], got Array”.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/types/types/fixed_array.rb', line 47 def describe_obj(obj) if obj.is_a?(Array) if obj.length == @types.length item_classes = obj.map(&:class).join(', ') "type [#{item_classes}]" else "array of size #{obj.length}" end else super end end |
#name ⇒ Object
16 17 18 |
# File 'lib/types/types/fixed_array.rb', line 16 def name "[#{@types.join(', ')}]" end |