Class: T::UnionType
- Inherits:
-
Object
- Object
- T::UnionType
- Defined in:
- lib/emery/type.rb
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #check(value) ⇒ Object
-
#initialize(*types) ⇒ UnionType
constructor
A new instance of UnionType.
- #to_s ⇒ Object
Constructor Details
#initialize(*types) ⇒ UnionType
Returns a new instance of UnionType.
39 40 41 |
# File 'lib/emery/type.rb', line 39 def initialize(*types) @types = types end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
38 39 40 |
# File 'lib/emery/type.rb', line 38 def types @types end |
Instance Method Details
#==(other) ⇒ Object
51 52 53 |
# File 'lib/emery/type.rb', line 51 def ==(other) T.instance_of?(UnionType, other) and (self.types - other.types).empty? end |
#check(value) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/emery/type.rb', line 45 def check(value) type = types.find {|t| T.instance_of?(t, value) } if type == nil raise TypeError.new("Value '#{value.inspect.to_s}' type is #{value.class} - any of #{@types.map { |t| t.to_s}.join(', ')} required") end end |
#to_s ⇒ Object
42 43 44 |
# File 'lib/emery/type.rb', line 42 def to_s "Union[#{types.map { |t| t.to_s}.join(', ')}]" end |