kind 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -1
- data/lib/kind/maybe.rb +55 -42
- data/lib/kind/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5426cb7f865320e961b1ad352dce372af3154afc520d1d9d4f64d7095ae0ddc2
|
4
|
+
data.tar.gz: b4ebb397ff928f49c2616b1464a964117b879c72ad37532381235d4e03e4d8f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 249dcd8cbb89239e035254ff28cffdd0d26a42e811fa86ae3682bf80d9ccf5154e129f1a5756e50ac4710db698361f0ab242d9018d3464384d72b7209d829bbe
|
7
|
+
data.tar.gz: 39723e362d309fddbd01b6732f962b60c361a29da36bf8ba73d9af65a2a579b750a98477c673d965bf3d81ef4cda60b215bf570d6aad00321bc44e497d997136
|
data/README.md
CHANGED
@@ -30,6 +30,7 @@ One of the goals of this project is to do simple type checking like `"some strin
|
|
30
30
|
- [Kind::Maybe](#kindmaybe)
|
31
31
|
- [Kind::Maybe[] and Kind::Maybe#then](#kindmaybe-and-kindmaybethen)
|
32
32
|
- [Kind::Maybe#try](#kindmaybetry)
|
33
|
+
- [Kind::Optional](#kindoptional)
|
33
34
|
- [Development](#development)
|
34
35
|
- [Contributing](#contributing)
|
35
36
|
- [License](#license)
|
@@ -276,7 +277,7 @@ The list of types (classes and modules) available to use with `Kind.of.*` or `Ki
|
|
276
277
|
- `Kind.is.Class()`
|
277
278
|
- `Kind.is.Module()`
|
278
279
|
- `Kind.is.Boolean()`
|
279
|
-
- `Kind.is.Callable()`: verifies if the given value `respond_to?(:call)` or if it's a class/module
|
280
|
+
- `Kind.is.Callable()`: verifies if the given value `respond_to?(:call)` or if it's a class/module and if its `public_instance_methods.include?(:call)`.
|
280
281
|
|
281
282
|
[⬆️ Back to Top](#table-of-contents-)
|
282
283
|
|
@@ -383,6 +384,33 @@ p Kind::Maybe[object].try { |value| value.upcase } # nil
|
|
383
384
|
|
384
385
|
[⬆️ Back to Top](#table-of-contents-)
|
385
386
|
|
387
|
+
### Kind::Optional
|
388
|
+
|
389
|
+
The `Kind::Optional` constant is an alias for `Kind::Maybe`. e.g:
|
390
|
+
|
391
|
+
```ruby
|
392
|
+
result1 =
|
393
|
+
Kind::Optional
|
394
|
+
.new(5)
|
395
|
+
.map { |value| value * 5 }
|
396
|
+
.map { |value| value - 10 }
|
397
|
+
.value_or(0)
|
398
|
+
|
399
|
+
puts result1 # 15
|
400
|
+
|
401
|
+
# ---
|
402
|
+
|
403
|
+
result2 =
|
404
|
+
Kind::Optional[5]
|
405
|
+
.then { |value| value * 5 }
|
406
|
+
.then { |value| value + 10 }
|
407
|
+
.value_or { 0 }
|
408
|
+
|
409
|
+
puts result2 # 35
|
410
|
+
```
|
411
|
+
|
412
|
+
[⬆️ Back to Top](#table-of-contents-)
|
413
|
+
|
386
414
|
## Development
|
387
415
|
|
388
416
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/kind/maybe.rb
CHANGED
@@ -2,21 +2,14 @@
|
|
2
2
|
|
3
3
|
module Kind
|
4
4
|
module Maybe
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def self.some?(value)
|
10
|
-
!none?(value)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.new(value)
|
14
|
-
result_type = some?(value) ? Some : None
|
15
|
-
result_type.new(value.is_a?(Result) ? value.value : value)
|
16
|
-
end
|
5
|
+
module Value
|
6
|
+
def self.none?(value)
|
7
|
+
value == nil || value == Undefined
|
8
|
+
end
|
17
9
|
|
18
|
-
|
19
|
-
|
10
|
+
def self.some?(value)
|
11
|
+
!none?(value)
|
12
|
+
end
|
20
13
|
end
|
21
14
|
|
22
15
|
class Result
|
@@ -26,17 +19,7 @@ module Kind
|
|
26
19
|
@value = value
|
27
20
|
end
|
28
21
|
|
29
|
-
|
30
|
-
|
31
|
-
def value_or(default = Undefined, &block)
|
32
|
-
return @value if some?
|
33
|
-
|
34
|
-
if default == Undefined && !block
|
35
|
-
raise ArgumentError, INVALID_DEFAULT_ARG
|
36
|
-
else
|
37
|
-
Maybe.some?(default) ? default : block.call
|
38
|
-
end
|
39
|
-
end
|
22
|
+
def value_or(default, &block); end
|
40
23
|
|
41
24
|
def none?; end
|
42
25
|
|
@@ -44,20 +27,20 @@ module Kind
|
|
44
27
|
|
45
28
|
def map(&fn); end
|
46
29
|
|
47
|
-
def try(method_name
|
48
|
-
|
30
|
+
def try(method_name, &block); end
|
31
|
+
end
|
49
32
|
|
50
|
-
|
51
|
-
result = fn.call(value)
|
33
|
+
private_constant :Result
|
52
34
|
|
53
|
-
|
54
|
-
|
55
|
-
end
|
35
|
+
class None < Result
|
36
|
+
INVALID_DEFAULT_ARG = 'the default value must be defined as an argument or block'.freeze
|
56
37
|
|
57
|
-
|
58
|
-
|
38
|
+
def value_or(default = Undefined, &block)
|
39
|
+
raise ArgumentError, INVALID_DEFAULT_ARG if default == Undefined && !block
|
40
|
+
|
41
|
+
Maybe::Value.some?(default) ? default : block.call
|
42
|
+
end
|
59
43
|
|
60
|
-
class None < Result
|
61
44
|
def none?; true; end
|
62
45
|
|
63
46
|
def map(&fn)
|
@@ -65,26 +48,56 @@ module Kind
|
|
65
48
|
end
|
66
49
|
|
67
50
|
alias_method :then, :map
|
51
|
+
|
52
|
+
def try(method_name = Undefined, &block)
|
53
|
+
Kind.of.Symbol(method_name) if method_name != Undefined
|
54
|
+
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
private_constant :INVALID_DEFAULT_ARG
|
68
59
|
end
|
69
60
|
|
70
|
-
|
71
|
-
|
61
|
+
NONE_WITH_NIL_VALUE = None.new(nil)
|
62
|
+
NONE_WITH_UNDEFINED_VALUE = None.new(Undefined)
|
63
|
+
|
64
|
+
private_constant :NONE_WITH_NIL_VALUE, :NONE_WITH_UNDEFINED_VALUE
|
72
65
|
|
73
66
|
class Some < Result
|
67
|
+
def value_or(default = Undefined, &block)
|
68
|
+
@value
|
69
|
+
end
|
70
|
+
|
74
71
|
def none?; false; end
|
75
72
|
|
76
73
|
def map(&fn)
|
77
|
-
result =
|
74
|
+
result = fn.call(@value)
|
75
|
+
|
76
|
+
return NONE_WITH_NIL_VALUE if result == nil
|
77
|
+
return NONE_WITH_UNDEFINED_VALUE if result == Undefined
|
78
78
|
|
79
|
-
|
80
|
-
return NONE_WITH_UNDEFINED if result == Undefined
|
81
|
-
return Some.new(result)
|
79
|
+
Some.new(result)
|
82
80
|
end
|
83
81
|
|
84
82
|
alias_method :then, :map
|
83
|
+
|
84
|
+
def try(method_name = Undefined, &block)
|
85
|
+
fn = method_name == Undefined ? block : Kind.of.Symbol(method_name).to_proc
|
86
|
+
|
87
|
+
result = fn.call(value)
|
88
|
+
|
89
|
+
return result if Maybe::Value.some?(result)
|
90
|
+
end
|
85
91
|
end
|
86
92
|
|
87
|
-
|
93
|
+
def self.new(value)
|
94
|
+
result_type = Maybe::Value.none?(value) ? None : Some
|
95
|
+
result_type.new(value.is_a?(Result) ? value.value : value)
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.[](value);
|
99
|
+
new(value)
|
100
|
+
end
|
88
101
|
end
|
89
102
|
|
90
103
|
Optional = Maybe
|
data/lib/kind/version.rb
CHANGED