kind 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dfe4558ce7a6bb67a5c7cb76d0ab12465ef644a909cc6f52e6c7890327db3a13
4
- data.tar.gz: 511872fbaaf0884287c2d5c8e8e0e871ba832c828892ab9124f38cb458b6a2d6
3
+ metadata.gz: 5426cb7f865320e961b1ad352dce372af3154afc520d1d9d4f64d7095ae0ddc2
4
+ data.tar.gz: b4ebb397ff928f49c2616b1464a964117b879c72ad37532381235d4e03e4d8f3
5
5
  SHA512:
6
- metadata.gz: 5d540ca9ec1ae62b1c560c9a3791742312832c7a4bd663cc319a5d2ac4ea7ed4a47c97de5810114d2a12f930bcf824513b058eaca37c3c736c136e5060eb55e1
7
- data.tar.gz: 3b3e80863478ca100746dfe778ea1cccf9a66d19fe4748b0f07e7c1f27e987b11bf673b35b8f797fcb21b693f69ad3692b39d2a4b8433dc302b33cf1dca15bfa
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, if its `public_instance_methods.include?(:call)`.
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.
@@ -2,21 +2,14 @@
2
2
 
3
3
  module Kind
4
4
  module Maybe
5
- def self.none?(value)
6
- value == nil || value == Undefined
7
- end
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
- def self.[](value);
19
- new(value)
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
- INVALID_DEFAULT_ARG = 'the default value must be defined as an argument or block'.freeze
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 = Undefined, &block)
48
- fn = method_name == Undefined ? block : Kind.of.Symbol(method_name).to_proc
30
+ def try(method_name, &block); end
31
+ end
49
32
 
50
- if Maybe.some?(value)
51
- result = fn.call(value)
33
+ private_constant :Result
52
34
 
53
- return result if Maybe.some?(result)
54
- end
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
- private_constant :INVALID_DEFAULT_ARG
58
- end
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
- NONE_WITH_NIL = None.new(nil)
71
- NONE_WITH_UNDEFINED = None.new(Undefined)
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 = yield(@value)
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
- return NONE_WITH_NIL if result == nil
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
- private_constant :Result, :NONE_WITH_NIL, :NONE_WITH_UNDEFINED
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kind
4
- VERSION = '1.4.0'
4
+ VERSION = '1.5.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kind
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura