operatic 0.5.0 → 0.6.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: 79842e69c42fad058cef240224911da273e0b801b51762bb4edc062590b1db1c
4
- data.tar.gz: d3262c93d6332c3833b3688425f09d1168bc961fc084b67bcaa148f2700ff60c
3
+ metadata.gz: 05be6b41b0969a2f2ae37af1a149095939d78b8724591d7598c46543f15d9070
4
+ data.tar.gz: 071dc0a5a5d652476a33498206e5e7a34e1c911cd6276018d9022b0558628815
5
5
  SHA512:
6
- metadata.gz: 212c3d737d571164069bacb305fe196008cbf27d1b81e440c230025715032a84c5df6e1dd2d2d6dc3a9073a5a9ebf010dff712e2f1577b68564f0d0d9594ca33
7
- data.tar.gz: 42d76bcf9a9e5ecef9ebb9e22d5eaa1378ed93bbbc164ac589acf134803b250c2ba8c6b461ef74e6927c6794d78c791d58811316c76ad6bc66fc0f44692326a2
6
+ metadata.gz: e76718b015b9f69c0d7fb8b9b600baacd95abfac4656b1c3d926f1c59b66e2d55e092f0c1cf2382978396fb03d65ce8a0f114f78eb9ee27879d28040e62426ad
7
+ data.tar.gz: f8adfcf7e3852cf6031ad407b1ea9f0cbdd82d9cd7983bf71fe5fab46c769997406ca58bccfcaddd2bc18ed8e828f29420b937ba15db3ffbdf8b2a5bbfba50aa
@@ -20,4 +20,8 @@ jobs:
20
20
  with:
21
21
  bundler-cache: true
22
22
  ruby-version: ${{ matrix.ruby }}
23
- - run: bundle exec rspec
23
+ - name: 'Set RSpec exclude pattern for Ruby < 2.7'
24
+ run: echo "::set-output name=value::**/*_gte_ruby_2_7_spec.rb"
25
+ if: matrix.ruby == '2.5' || matrix.ruby == '2.6'
26
+ id: rspec_exclude_pattern
27
+ - run: bundle exec rspec --exclude-pattern=${{ steps.rspec_exclude_pattern.outputs.value }}
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## Version 0.6.0 - 2022-08-22
4
+
5
+ - Support pattern matching a Result (in Ruby 2.7+). <https://github.com/benpickles/operatic/pull/12>
6
+
3
7
  ## Version 0.5.0 - 2022-06-23
4
8
 
5
9
  - Support custom initialize method to aid compatibility with other libraries. <https://github.com/benpickles/operatic/pull/11>
data/README.md CHANGED
@@ -49,6 +49,17 @@ result[:message] # => nil
49
49
  result.to_h # => {}
50
50
  ```
51
51
 
52
+ An `Operatic::Result` also supports pattern matching in Ruby 2.7+ returning an array of `[success, data]`:
53
+
54
+ ```ruby
55
+ case SayHello.call(name: 'Dave')
56
+ in [true, { message: }]
57
+ # Result is a success, do something with the `message` variable.
58
+ in [false, _]
59
+ # Result is a failure, do something else.
60
+ end
61
+ ```
62
+
52
63
  A Rails controller might use Operatic like this:
53
64
 
54
65
  ```ruby
@@ -65,6 +76,35 @@ class HellosController < ApplicationController
65
76
  end
66
77
  ```
67
78
 
79
+ Or a pattern matching alternative:
80
+
81
+ ```ruby
82
+ class HellosController < ApplicationController
83
+ def create
84
+ case SayHello.call(name: params[:name])
85
+ in [true, { message: }]
86
+ render plain: message
87
+ in [false, _]
88
+ render :new
89
+ end
90
+ end
91
+ end
92
+ ```
93
+
94
+ ## Development
95
+
96
+ Run the tests with:
97
+
98
+ ```
99
+ bundle exec rspec
100
+ ```
101
+
102
+ Generate Yard documentation with:
103
+
104
+ ```
105
+ bundle exec yardoc
106
+ ```
107
+
68
108
  ## License
69
109
 
70
110
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -34,14 +34,31 @@ module Operatic
34
34
  @data[key] = value
35
35
  end
36
36
 
37
+ # Returns an array of success and data.
38
+ #
39
+ # @example
40
+ # result = Result.new.success!(message: 'Hello world')
41
+ #
42
+ # case result
43
+ # in [true, { message: }]
44
+ # # Result is a success, do something with the `message` variable.
45
+ # in [false, _]
46
+ # # Result is a failure, do something else.
47
+ # end
48
+ #
49
+ # @return [Array(Boolean, Hash<Symbol, anything>)]
50
+ def deconstruct
51
+ [@success, @data]
52
+ end
53
+
37
54
  # Mark the result as a failure, optionally attach +data+ via kwargs, and
38
55
  # freeze the object so it cannot be modified further.
39
56
  #
40
57
  # *Note*: Calling {#success!} or {#failure!} more than once will raise a
41
58
  # +FrozenError+.
42
59
  def failure!(**data)
43
- set_data(**data)
44
60
  @success = false
61
+ set_data(data)
45
62
  freeze
46
63
  end
47
64
 
@@ -64,8 +81,8 @@ module Operatic
64
81
  # *Note*: Calling {#success!} or {#failure!} more than once will raise a
65
82
  # +FrozenError+.
66
83
  def success!(**data)
67
- set_data(**data)
68
84
  @success = true
85
+ set_data(data)
69
86
  freeze
70
87
  end
71
88
 
@@ -82,7 +99,7 @@ module Operatic
82
99
  end
83
100
 
84
101
  private
85
- def set_data(**data)
102
+ def set_data(data)
86
103
  data.each do |key, value|
87
104
  @data[key] = value
88
105
  end
@@ -1,3 +1,3 @@
1
1
  module Operatic
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: operatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Pickles
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-23 00:00:00.000000000 Z
11
+ date: 2022-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler