Class: T::Types::Union
Overview
Takes a list of types. Validates that an object matches at least one of the types.
Defined Under Namespace
Modules: Private
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#initialize(types) ⇒ Union
constructor
A new instance of Union.
-
#name ⇒ Object
overrides Base.
-
#recursively_valid?(obj) ⇒ Boolean
overrides Base.
-
#valid?(obj) ⇒ Boolean
overrides Base.
Methods inherited from Base
#==, #describe_obj, #error_message_for_obj, #error_message_for_obj_recursive, #hash, method_added, #subtype_of?, #to_s, #validate!
Constructor Details
#initialize(types) ⇒ Union
Returns a new instance of Union.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/types/types/union.rb', line 9 def initialize(types) @types = types.flat_map do |type| type = T::Utils.coerce(type) if type.is_a?(Union) # Simplify nested unions (mostly so `name` returns a nicer value) type.types else type end end.uniq end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
7 8 9 |
# File 'lib/types/types/union.rb', line 7 def types @types end |
Instance Method Details
#name ⇒ Object
overrides Base
22 23 24 |
# File 'lib/types/types/union.rb', line 22 def name type_shortcuts(@types) end |
#recursively_valid?(obj) ⇒ Boolean
overrides Base
46 47 48 |
# File 'lib/types/types/union.rb', line 46 def recursively_valid?(obj) @types.any? {|type| type.recursively_valid?(obj)} end |
#valid?(obj) ⇒ Boolean
overrides Base
51 52 53 |
# File 'lib/types/types/union.rb', line 51 def valid?(obj) @types.any? {|type| type.valid?(obj)} end |