Skip to content

Commit 636ab2e

Browse files
committed
Renamed Union to Any
1 parent 68ef60c commit 636ab2e

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

README.md

+42-4
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,32 @@ sig validate: T.Boolean
7171
def create(validate:); end
7272
```
7373
74-
#### `Union`
74+
#### `Any`
7575
76-
Value **MUST** be a union of the given list of values, that is, the value must be one of the given list.
76+
Value **MUST** be any of the given list of values, that is, the value must be one of the given list.
7777
7878
```ruby
79-
sig T.Union(:male, :female)
79+
sig T.Any(:male, :female)
8080
def create(gender); end
8181
```
8282
83+
If no type is given, the value **CAN** be any type or value.
84+
85+
```ruby
86+
sig save: T.Any
87+
def create(save: nil); end
88+
```
89+
90+
You can also pass `nil` to allow a nil value alongside any other types you provide.
91+
92+
```ruby
93+
sig T.Any(String, nil)
94+
def create(save = nil); end
95+
```
96+
8397
#### `Nilable`
8498
85-
When a type is given, the value **MUST** be nil or of the given type.
99+
When a type is given, the value **MUST** be nil **OR** of the given type.
86100
87101
```ruby
88102
sig save: T.Nilable(String)
@@ -98,3 +112,27 @@ If no type is given, the value **CAN** be nil. This essentially allows any value
98112
sig save: T.Nilable
99113
def create(save: nil); end
100114
```
115+
116+
You may notice that `Nilable` is interchangeable with `Any`. The following are equivilent:
117+
118+
```ruby
119+
sig save: T.Nilable
120+
def create(save: nil); end
121+
```
122+
123+
```ruby
124+
sig save: T.Any
125+
def create(save: nil); end
126+
```
127+
128+
As are these:
129+
130+
```ruby
131+
sig T.Nilable(String)
132+
def update(name = nil); end
133+
```
134+
135+
```ruby
136+
sig T.Any(String, nil)
137+
def update(name = nil); end
138+
```

lib/delivered/types.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# frozen_string_literal: true
22

33
module Delivered
4-
class UnionType
4+
class AnyType
55
def initialize(*types)
66
@types = types
77
end
88

99
def ===(value)
10-
@types.any? { |type| type === value }
10+
@types.empty? ? true : @types.any? { |type| type === value }
1111
end
1212
end
1313

@@ -35,7 +35,7 @@ module Types
3535
module_function
3636

3737
def Nilable(type = nil) = NilableType.new(type)
38-
def Union(*types) = UnionType.new(*types)
38+
def Any(*types) = AnyType.new(*types)
3939
def Boolean = BooleanType.new
4040
end
4141
end

test/delivered/types.rb

+24-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,32 @@
33
describe Delivered::Types do
44
T = Delivered::Types
55

6-
describe 'Union' do
7-
it 'is within union' do
8-
assert T.Union(:one, :two) === :one
6+
describe 'Any' do
7+
with 'no arguments' do
8+
it 'any type or nil' do
9+
assert T.Any === :one
10+
assert T.Any === 'one'
11+
assert T.Any === nil
12+
end
13+
end
14+
15+
with 'arguments' do
16+
it 'one of given arg' do
17+
assert T.Any(:one, :two) === :one
18+
assert T.Any(String, Integer) === 'one'
19+
assert T.Any(String, Integer) === 1
20+
assert T.Any(Integer, nil) === nil
21+
end
922
end
1023

11-
it 'raises when value is not in union' do
12-
expect { :three => ^(T.Union(:one, :two)) }.to raise_exception NoMatchingPatternError
13-
expect { :one => ^(T.Union(:one, :two)) }.not.to raise_exception
24+
it 'raises when value is not valid' do
25+
expect { :three => ^(T.Any(:one, :two)) }.to raise_exception NoMatchingPatternError
26+
expect { :one => ^(T.Any(:one, :two)) }.not.to raise_exception
27+
expect { 'one' => ^(T.Any(Symbol, String)) }.not.to raise_exception
28+
expect { 'one' => ^(T.Any) }.not.to raise_exception
29+
expect { nil => ^(T.Any) }.not.to raise_exception
30+
expect { nil => ^(T.Any(String, nil)) }.not.to raise_exception
31+
expect { true => ^(T.Any(String, nil)) }.to raise_exception NoMatchingPatternError
1432
end
1533
end
1634

0 commit comments

Comments
 (0)