Class: TSJSON::LiteralUnion
- Inherits:
-
Base
- Object
- Base
- TSJSON::LiteralUnion
show all
- Defined in:
- lib/types/literal_union.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Base
#compile, #index, #property, #valid?
Constructor Details
#initialize(types = []) ⇒ LiteralUnion
Returns a new instance of LiteralUnion.
5
6
7
8
9
|
# File 'lib/types/literal_union.rb', line 5
def initialize(types = [])
types.each { |t| validate_type!(t) }
@types = types
end
|
Instance Attribute Details
#types ⇒ Object
Returns the value of attribute types.
3
4
5
|
# File 'lib/types/literal_union.rb', line 3
def types
@types
end
|
Instance Method Details
#has?(type) ⇒ Boolean
17
18
19
|
# File 'lib/types/literal_union.rb', line 17
def has?(type)
@types.include?(type)
end
|
#push_type(type) ⇒ Object
11
12
13
14
15
|
# File 'lib/types/literal_union.rb', line 11
def push_type(type)
validate_type!(type)
return self if types.include?(type)
return LiteralUnion.new(@types.dup.push(type))
end
|
#size ⇒ Object
21
22
23
|
# File 'lib/types/literal_union.rb', line 21
def size
@types.size
end
|
#to_json ⇒ Object
44
45
46
|
# File 'lib/types/literal_union.rb', line 44
def to_json
@types.map(&:value)
end
|
#to_s ⇒ Object
40
41
42
|
# File 'lib/types/literal_union.rb', line 40
def to_s
@types.map(&:to_s).join(' | ')
end
|
#validate(value) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'lib/types/literal_union.rb', line 31
def validate(value)
@types.each { |type| return true if type.valid?(value) }
raise LiteralUnionValidationError.new(
expected_values: @types.map(&:value),
received_value: value
)
end
|
#validate_type!(type) ⇒ Object
25
26
27
28
29
|
# File 'lib/types/literal_union.rb', line 25
def validate_type!(type)
unless type.is_a?(Literal)
raise 'LiteralUnion may contain only Literal types'
end
end
|