Class: YardTypes::CollectionType
Overview
The current implementation of type checking here requires that the collection respond to all?; this may not be ideal.
A CollectionType is specified with the syntax Kind<Some, #thing>, and indicates that the object is a kind of Kind, containing only objects which type check against Some or #thing.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#types ⇒ Array<Type>
The acceptable types for this collection’s contents.
Attributes inherited from Type
Instance Method Summary collapse
-
#check(obj) ⇒ Boolean
trueif the object is both a kind ofname, and all of its contents (if any) are of the types intypes. -
#initialize(name, types) ⇒ CollectionType
constructor
A new instance of CollectionType.
-
#to_s ⇒ String
A YARD type string describing this type.
Methods inherited from Type
Constructor Details
#initialize(name, types) ⇒ CollectionType
Returns a new instance of CollectionType.
183 184 185 186 |
# File 'lib/yard_types/types.rb', line 183 def initialize(name, types) @name = name @types = types end |
Instance Attribute Details
#types ⇒ Array<Type>
Returns the acceptable types for this collection’s contents.
179 180 181 |
# File 'lib/yard_types/types.rb', line 179 def types @types end |
Instance Method Details
#check(obj) ⇒ Boolean
Returns true if the object is both a kind of name, and all of its contents (if any) are of the types in types. Any combination, order, and count of content types is acceptable.
197 198 199 200 201 202 203 204 |
# File 'lib/yard_types/types.rb', line 197 def check(obj) return false unless KindType.new(name).check(obj) obj.all? do |el| # TODO -- could probably just use another TypeConstraint here types.any? { |type| type.check(el) } end end |
#to_s ⇒ String
Returns a YARD type string describing this type.
189 190 191 |
# File 'lib/yard_types/types.rb', line 189 def to_s "%s<%s>" % [name, types.map(&:to_s).join(', ')] end |