Class: YardTypes::CollectionType
- Includes:
- OrList
- Defined in:
- lib/yard_types/types.rb
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. -
#description ⇒ String
An English phrase describing this type.
-
#initialize(name, types) ⇒ CollectionType
constructor
A new instance of CollectionType.
-
#to_s ⇒ String
A YARD type string describing this type.
Methods included from OrList
Methods inherited from Type
Constructor Details
#initialize(name, types) ⇒ CollectionType
Returns a new instance of CollectionType.
218 219 220 221 |
# File 'lib/yard_types/types.rb', line 218 def initialize(name, types) @name = name @types = types end |
Instance Attribute Details
#types ⇒ Array<Type>
Returns the acceptable types for this collection's contents.
214 215 216 |
# File 'lib/yard_types/types.rb', line 214 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.
239 240 241 242 243 244 245 246 |
# File 'lib/yard_types/types.rb', line 239 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 |
#description ⇒ String
Returns an English phrase describing this type.
229 230 231 232 233 |
# File 'lib/yard_types/types.rb', line 229 def description article = name[0] =~ /[aeiou]/i ? 'an' : 'a' type_descriptions = types.map(&:description) "#{article} #{name} of (#{or_list(type_descriptions)})" end |
#to_s ⇒ String
Returns a YARD type string describing this type.
224 225 226 |
# File 'lib/yard_types/types.rb', line 224 def to_s "%s<%s>" % [name, types.map(&:to_s).join(', ')] end |