Class: Literal::Union

Inherits:
Type
  • Object
show all
Includes:
Enumerable
Defined in:
lib/literal/union.rb

Instance Method Summary collapse

Methods included from Modifiers

extended

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

#deconstructObject



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

#eachObject



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

#indexObject



24
25
26
# File 'lib/literal/union.rb', line 24

def index
	@index ||= @types.index_by(&:itself)
end

#inspectObject



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