bcdd-result 0.3.0 → 0.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: 96c58d6a9132fbf42fc8f3c1d9ba683f0b6297e23b1f0536fc4bcb7efb5084df
4
- data.tar.gz: fcef45f3c5ddacc20ff5e9533012c7fbe5817878cef1ff528dabd8920e1cbccd
3
+ metadata.gz: b4eb0878d2e7d75bb4cfd55c0d112db0f14b4c43255d3e02d60ec60c0f9c2d6e
4
+ data.tar.gz: dd8ee0d7239a63b486de7efb7876c40db1942f2d54a383ed0c67bb86e20b0622
5
5
  SHA512:
6
- metadata.gz: d4cf048907571205c6cd0b90ca4b95c2480c34de79d082132e4f1cab1e809f4c6eec538d9d38b036ad313d9653420625b68a502272e73371b99b157f43315123
7
- data.tar.gz: 4662aaeb33a7eb770ab51529cc1f2c4e9d86844dec70dfcdc4f37674aab33d010d6cbe7932fc635f09979d47b4b631748a3d4791021e99108a011a93469a2d73
6
+ metadata.gz: '08f8ed0b78bcf298711a52b20be08908dc3000888a94b8bd3df289fa3682909bb87ba294c2ba42a482678af0f9dd5bff5a267169a256ca7cb0a23c40b961613c'
7
+ data.tar.gz: cc15020416823a965c29ece5947875817df0ccc6b7c21f4dc534ba24aa68e10134a4333d0e36bcb8afc08719f9e6aeea1741ea51be6b373cc8efc61e82b08a7f
data/.rubocop.yml CHANGED
@@ -18,9 +18,30 @@ Layout/ExtraSpacing:
18
18
  Style/ClassAndModuleChildren:
19
19
  Enabled: false
20
20
 
21
+ Style/MapToSet:
22
+ Exclude:
23
+ - lib/bcdd/result/expectations/contract/for_types.rb
24
+
25
+ Style/CaseEquality:
26
+ Exclude:
27
+ - lib/bcdd/result/expectations/contract/for_types_and_values.rb
28
+
29
+ Style/Lambda:
30
+ EnforcedStyle: literal
31
+
21
32
  Naming/MethodName:
22
33
  Exclude:
23
- - lib/bcdd/resultable.rb
34
+ - lib/bcdd/result/mixin.rb
35
+ - lib/bcdd/result/expectations.rb
24
36
 
25
37
  Minitest/MultipleAssertions:
26
38
  Enabled: false
39
+
40
+ Metrics/BlockLength:
41
+ Exclude:
42
+ - bcdd-result.gemspec
43
+ - test/**/*.rb
44
+
45
+ Metrics/ClassLength:
46
+ Exclude:
47
+ - test/**/*.rb
data/.rubocop_todo.yml CHANGED
@@ -1,21 +1,12 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2023-09-27 00:47:03 UTC using RuboCop version 1.56.3.
3
+ # on 2023-10-02 02:37:50 UTC using RuboCop version 1.56.3.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 13
9
+ # Offense count: 24
10
10
  # Configuration parameters: AllowedConstants.
11
11
  Style/Documentation:
12
- Exclude:
13
- - 'spec/**/*'
14
- - 'test/**/*'
15
- - 'lib/bcdd/result.rb'
16
- - 'lib/bcdd/result/error.rb'
17
- - 'lib/bcdd/result/failure.rb'
18
- - 'lib/bcdd/result/handler.rb'
19
- - 'lib/bcdd/result/success.rb'
20
- - 'lib/bcdd/result/type.rb'
21
- - 'lib/bcdd/resultable.rb'
12
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,101 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.0] - 2023-10-09
4
+
5
+ - Add `BCDD::Result::Expectations` to define contracts for your results. There are two ways to use it: the standalone (`BCDD::Result::Expectations.new`) and the mixin (`BCDD::Result::Expectations.mixin`) mode.
6
+
7
+ The main difference is that the mixin mode will use the target object (who receives the include/extend) as the result's subject (like the `BCDD::Result::Mixin` does), while the standalone mode won't.
8
+
9
+ **Standalone mode:**
10
+
11
+ ```ruby
12
+ module Divide
13
+ Expected = BCDD::Result::Expectations.new(
14
+ success: {
15
+ numbers: ->(value) { value.is_a?(Array) && value.size == 2 && value.all?(Numeric) },
16
+ division_completed: Numeric
17
+ },
18
+ failure: {
19
+ invalid_arg: String,
20
+ division_by_zero: String
21
+ }
22
+ )
23
+
24
+ def self.call(arg1, arg2)
25
+ arg1.is_a?(Numeric) or return Expected::Failure(:invalid_arg, 'arg1 must be numeric')
26
+ arg2.is_a?(Numeric) or return Expected::Failure(:invalid_arg, 'arg2 must be numeric')
27
+
28
+ arg2.zero? and return Expected::Failure(:division_by_zero, 'arg2 must not be zero')
29
+
30
+ Expected::Success(:division_completed, arg1 / arg2)
31
+ end
32
+ end
33
+ ```
34
+
35
+ **Mixin mode:**
36
+
37
+ ```ruby
38
+ class Divide
39
+ include BCDD::Result::Expectations.mixin(
40
+ success: {
41
+ numbers: ->(value) { value.is_a?(Array) && value.size == 2 && value.all?(Numeric) },
42
+ division_completed: Numeric
43
+ },
44
+ failure: {
45
+ invalid_arg: String,
46
+ division_by_zero: String
47
+ }
48
+ )
49
+
50
+ def call(arg1, arg2)
51
+ validate_numbers(arg1, arg2)
52
+ .and_then(:validate_non_zero)
53
+ .and_then(:divide)
54
+ end
55
+
56
+ private
57
+
58
+ def validate_numbers(arg1, arg2)
59
+ arg1.is_a?(Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric')
60
+ arg2.is_a?(Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric')
61
+
62
+ Success(:numbers, [arg1, arg2])
63
+ end
64
+
65
+ def validate_non_zero(numbers)
66
+ return Success(:numbers, numbers) unless numbers.last.zero?
67
+
68
+ Failure(:division_by_zero, 'arg2 must not be zero')
69
+ end
70
+
71
+ def divide((number1, number2))
72
+ Success(:division_completed, number1 / number2)
73
+ end
74
+ end
75
+ ```
76
+
77
+ ## [0.4.0] - 2023-09-28
78
+
79
+ ### Added
80
+
81
+ - Add `require 'result'` to define `Result` as an alias for `BCDD::Result`.
82
+
83
+ - Add support to pattern matching (Ruby 2.7+).
84
+
85
+ - Add `BCDD::Result#on_unknown` to execute a block if no other hook (`#on`, `#on_type`, `#on_failure`, `#on_success`) has been executed. Attention: always use it as the last hook.
86
+
87
+ - Add `BCDD::Result::Handler#unknown` to execute a block if no other handler (`#[]`, `#type`, `#failure`, `#success`) has been executed. Attention: always use it as the last handler.
88
+
89
+ ### Changed
90
+
91
+ - **(BREAKING)** Rename `BCDD::Resultable` to `BCDD::Result::Mixin`.
92
+
93
+ - **(BREAKING)** Change `BCDD::Result#data` to return a `BCDD::Result::Data` instead of the result value. This object exposes the result attributes (name, type, value) directly and as a hash (`to_h`/`to_hash`) and array (`to_a`/`to_ary`).
94
+
95
+ ### Removed
96
+
97
+ - **(BREAKING)** Remove `BCDD::Result#data_or`.
98
+
3
99
  ## [0.3.0] - 2023-09-26
4
100
 
5
101
  ### Added