Class: Literal::Union
- Inherits:
-
Type
- Object
- Type
- Literal::Union
- Includes:
- Enumerable
- Defined in:
- lib/literal/union.rb
Instance Method Summary collapse
- #===(value) ⇒ Object
- #[](key) ⇒ Object
- #deconstruct ⇒ Object
- #deconstruct_keys(_) ⇒ Object
- #each ⇒ Object
- #handle(value) ⇒ Object
- #index ⇒ Object
-
#initialize(*types) ⇒ Union
constructor
A new instance of Union.
- #inspect ⇒ Object
- #variant(value = Literal::Null) ⇒ Object
Methods included from Modifiers
Constructor Details
#initialize(*types) ⇒ Union
Returns a new instance of Union.
6 7 8 |
# File 'lib/literal/union.rb', line 6 def initialize(*types) @types = types end |
Instance Method Details
#===(value) ⇒ Object
12 13 14 |
# File 'lib/literal/union.rb', line 12 def ===(value) @types.any? { |type| type === value } end |
#[](key) ⇒ Object
20 21 22 |
# File 'lib/literal/union.rb', line 20 def [](key) index.fetch(key) end |
#deconstruct ⇒ Object
42 43 44 |
# File 'lib/literal/union.rb', line 42 def deconstruct @types end |
#deconstruct_keys(_) ⇒ Object
46 47 48 |
# File 'lib/literal/union.rb', line 46 def deconstruct_keys(_) { types: @types } end |
#each ⇒ Object
16 17 18 |
# File 'lib/literal/union.rb', line 16 def each(&) @types.each(&) end |
#handle(value) ⇒ Object
28 29 30 |
# File 'lib/literal/union.rb', line 28 def handle(value, &) Literal::Variant.new(value, *@types).handle(&) end |
#index ⇒ Object
24 25 26 |
# File 'lib/literal/union.rb', line 24 def index @index ||= @types.index_by(&:itself) end |
#inspect ⇒ Object
10 |
# File 'lib/literal/union.rb', line 10 def inspect = "Literal::Union(#{@types.map(&:inspect).join(', ')})" |
#variant(value = Literal::Null) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/literal/union.rb', line 32 def variant(value = Literal::Null) if Literal::Null != value Literal::Variant.new(value, *@types) elsif block_given? Literal::Variant.new(yield, *@types) else Literal::VariantType.new(*@types) end end |