Class: T::Types::Intersection
- Defined in:
- lib/types/types/intersection.rb
Overview
Takes a list of types. Validates that an object matches all of the types.
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#initialize(types) ⇒ Intersection
constructor
A new instance of Intersection.
- #name ⇒ Object
- #valid?(obj) ⇒ Boolean
Methods inherited from Base
#==, #describe_obj, #error_message_for_obj, #hash, method_added, #subtype_of?, #to_s, #validate!
Constructor Details
#initialize(types) ⇒ Intersection
Returns a new instance of Intersection.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/types/types/intersection.rb', line 9 def initialize(types) @types = types.flat_map do |type| if type.is_a?(Intersection) # Simplify nested intersections (mostly so `name` returns a nicer value) type.types else T::Utils.coerce(type) end end.uniq end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
7 8 9 |
# File 'lib/types/types/intersection.rb', line 7 def types @types end |
Instance Method Details
#name ⇒ Object
21 22 23 |
# File 'lib/types/types/intersection.rb', line 21 def name "T.all(#{@types.map(&:name).sort.join(', ')})" end |
#valid?(obj) ⇒ Boolean
26 27 28 |
# File 'lib/types/types/intersection.rb', line 26 def valid?(obj) @types.all? {|type| type.valid?(obj)} end |