operatic 0.3.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +10 -18
- data/CHANGELOG.md +14 -0
- data/README.md +10 -8
- data/lib/operatic/result.rb +27 -23
- data/lib/operatic/version.rb +1 -1
- data/lib/operatic.rb +34 -27
- data/operatic.gemspec +1 -1
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79842e69c42fad058cef240224911da273e0b801b51762bb4edc062590b1db1c
|
4
|
+
data.tar.gz: d3262c93d6332c3833b3688425f09d1168bc961fc084b67bcaa148f2700ff60c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 212c3d737d571164069bacb305fe196008cbf27d1b81e440c230025715032a84c5df6e1dd2d2d6dc3a9073a5a9ebf010dff712e2f1577b68564f0d0d9594ca33
|
7
|
+
data.tar.gz: 42d76bcf9a9e5ecef9ebb9e22d5eaa1378ed93bbbc164ac589acf134803b250c2ba8c6b461ef74e6927c6794d78c791d58811316c76ad6bc66fc0f44692326a2
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
name: Ruby
|
2
2
|
|
3
|
-
on:
|
3
|
+
on: push
|
4
4
|
|
5
5
|
jobs:
|
6
6
|
rspec:
|
@@ -8,24 +8,16 @@ jobs:
|
|
8
8
|
strategy:
|
9
9
|
matrix:
|
10
10
|
ruby:
|
11
|
-
- '2.5
|
12
|
-
- '2.6
|
13
|
-
|
11
|
+
- '2.5'
|
12
|
+
- '2.6'
|
13
|
+
- '2.7'
|
14
|
+
- '3.0'
|
15
|
+
- '3.1'
|
14
16
|
name: Ruby ${{ matrix.ruby }} RSpec
|
15
|
-
|
16
17
|
steps:
|
17
|
-
- uses: actions/checkout@
|
18
|
-
|
19
|
-
- name: Set up Ruby
|
20
|
-
uses: actions/setup-ruby@v1
|
18
|
+
- uses: actions/checkout@v2
|
19
|
+
- uses: ruby/setup-ruby@v1
|
21
20
|
with:
|
21
|
+
bundler-cache: true
|
22
22
|
ruby-version: ${{ matrix.ruby }}
|
23
|
-
|
24
|
-
- name: Install latest bundler
|
25
|
-
run: gem install bundler
|
26
|
-
|
27
|
-
- name: Install operatic dependencies
|
28
|
-
run: bundle install --jobs 4
|
29
|
-
|
30
|
-
- name: Run RSpec
|
31
|
-
run: bundle exec rspec
|
23
|
+
- run: bundle exec rspec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## Version 0.5.0 - 2022-06-23
|
4
|
+
|
5
|
+
- Support custom initialize method to aid compatibility with other libraries. <https://github.com/benpickles/operatic/pull/11>
|
6
|
+
- Rename to `Operatic.result_attr` to be more specific about its functionality. <https://github.com/benpickles/operatic/pull/10>
|
7
|
+
- Get and set Result data with `#[]` / `#[]=`. <https://github.com/benpickles/operatic/pull/9>
|
8
|
+
|
9
|
+
## Version 0.4.0 - 2022-05-25
|
10
|
+
|
11
|
+
- Switch to keyword arguments. <https://github.com/benpickles/operatic/pull/8>
|
12
|
+
|
13
|
+
## Version 0.3.1 - 2020-01-05
|
14
|
+
|
15
|
+
- Less specific Rake dependency. <https://github.com/benpickles/operatic/pull/6>
|
16
|
+
|
3
17
|
## Version 0.3.0 - 2019-11-27
|
4
18
|
|
5
19
|
- Implement `#to_h` not `#to_hash`. <https://github.com/benpickles/operatic/pull/4>
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ class SayHello
|
|
24
24
|
attr_reader :name
|
25
25
|
|
26
26
|
# Declare convenience accessors on the result.
|
27
|
-
|
27
|
+
result_attr :message
|
28
28
|
|
29
29
|
def call
|
30
30
|
# Exit the method and mark the result as a failure.
|
@@ -36,15 +36,17 @@ class SayHello
|
|
36
36
|
end
|
37
37
|
|
38
38
|
result = SayHello.call(name: 'Dave')
|
39
|
-
result.success?
|
40
|
-
result.message
|
41
|
-
result
|
39
|
+
result.success? # => true
|
40
|
+
result.message # => "Hello Dave"
|
41
|
+
result[:message] # => "Hello Dave"
|
42
|
+
result.to_h # => {:message=>"Hello Dave"}
|
42
43
|
|
43
44
|
result = SayHello.call
|
44
|
-
result.failure?
|
45
|
-
result.success?
|
46
|
-
result.message
|
47
|
-
result
|
45
|
+
result.failure? # => true
|
46
|
+
result.success? # => false
|
47
|
+
result.message # => nil
|
48
|
+
result[:message] # => nil
|
49
|
+
result.to_h # => {}
|
48
50
|
```
|
49
51
|
|
50
52
|
A Rails controller might use Operatic like this:
|
data/lib/operatic/result.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Operatic
|
2
2
|
class Result
|
3
3
|
# Generate a subclass of {Result} with named +attrs+ accessors. This
|
4
|
-
# wouldn't normally be called directly, see {ClassMethods#
|
4
|
+
# wouldn't normally be called directly, see {ClassMethods#result_attr} for
|
5
5
|
# example usage.
|
6
6
|
#
|
7
|
-
# @param attrs [Array<Symbol>] a list of
|
7
|
+
# @param attrs [Array<Symbol>] a list of convenience data accessors.
|
8
8
|
def self.generate(*attrs)
|
9
9
|
Class.new(self) do
|
10
10
|
attrs.each do |name|
|
@@ -24,16 +24,23 @@ module Operatic
|
|
24
24
|
@success = true
|
25
25
|
end
|
26
26
|
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
# Read data that's attached to the result.
|
28
|
+
def [](key)
|
29
|
+
@data[key]
|
30
|
+
end
|
31
|
+
|
32
|
+
# Set data on the result.
|
33
|
+
def []=(key, value)
|
34
|
+
@data[key] = value
|
35
|
+
end
|
36
|
+
|
37
|
+
# Mark the result as a failure, optionally attach +data+ via kwargs, and
|
38
|
+
# freeze the object so it cannot be modified further.
|
32
39
|
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
def failure!(data
|
36
|
-
set_data(data)
|
40
|
+
# *Note*: Calling {#success!} or {#failure!} more than once will raise a
|
41
|
+
# +FrozenError+.
|
42
|
+
def failure!(**data)
|
43
|
+
set_data(**data)
|
37
44
|
@success = false
|
38
45
|
freeze
|
39
46
|
end
|
@@ -47,20 +54,17 @@ module Operatic
|
|
47
54
|
super
|
48
55
|
end
|
49
56
|
|
50
|
-
# Mark the result as a success, optionally attach data, and
|
51
|
-
# object so it cannot be modified further.
|
57
|
+
# Mark the result as a success, optionally attach +data+ via kwargs, and
|
58
|
+
# freeze the object so it cannot be modified further.
|
52
59
|
#
|
53
|
-
# Calling this is not strictly necessary as a
|
60
|
+
# Calling this is not strictly necessary as a +Result+ defaults to being a
|
54
61
|
# success, but it's a convenient means of attaching data and of indicating
|
55
62
|
# intent in the consuming code.
|
56
63
|
#
|
57
|
-
# *Note*:
|
58
|
-
#
|
59
|
-
|
60
|
-
|
61
|
-
# to the result.
|
62
|
-
def success!(data = nil)
|
63
|
-
set_data(data) if data
|
64
|
+
# *Note*: Calling {#success!} or {#failure!} more than once will raise a
|
65
|
+
# +FrozenError+.
|
66
|
+
def success!(**data)
|
67
|
+
set_data(**data)
|
64
68
|
@success = true
|
65
69
|
freeze
|
66
70
|
end
|
@@ -72,13 +76,13 @@ module Operatic
|
|
72
76
|
# Returns the full (frozen) hash of data attached to the result via
|
73
77
|
# {#success!}, {#failure!}, or convenience accessors added with {.generate}.
|
74
78
|
#
|
75
|
-
# @return [Hash]
|
79
|
+
# @return [Hash<Symbol, anything>]
|
76
80
|
def to_h
|
77
81
|
@data
|
78
82
|
end
|
79
83
|
|
80
84
|
private
|
81
|
-
def set_data(data)
|
85
|
+
def set_data(**data)
|
82
86
|
data.each do |key, value|
|
83
87
|
@data[key] = value
|
84
88
|
end
|
data/lib/operatic/version.rb
CHANGED
data/lib/operatic.rb
CHANGED
@@ -11,15 +11,12 @@ module Operatic
|
|
11
11
|
module ClassMethods
|
12
12
|
# The main way of calling an operation.
|
13
13
|
#
|
14
|
-
# The class is instantiated with supplied +attrs+
|
15
|
-
# returning a frozen {Result} instance.
|
14
|
+
# The class is instantiated with the supplied +attrs+ keyword arguments and
|
15
|
+
# calls {Operatic#call} returning a frozen {Result} instance.
|
16
16
|
#
|
17
|
-
# @
|
18
|
-
|
19
|
-
|
20
|
-
# @return a [Result]
|
21
|
-
def call(attrs = nil)
|
22
|
-
new(attrs)
|
17
|
+
# @return [Result]
|
18
|
+
def call(**attrs)
|
19
|
+
new(**attrs)
|
23
20
|
.tap(&:call)
|
24
21
|
.result
|
25
22
|
.freeze
|
@@ -30,8 +27,8 @@ module Operatic
|
|
30
27
|
# test setups, etc.
|
31
28
|
#
|
32
29
|
# @return [Result]
|
33
|
-
def call!(attrs
|
34
|
-
call(attrs).tap { |result|
|
30
|
+
def call!(**attrs)
|
31
|
+
call(**attrs).tap { |result|
|
35
32
|
raise FailureError if result.failure?
|
36
33
|
}
|
37
34
|
end
|
@@ -45,7 +42,7 @@ module Operatic
|
|
45
42
|
#
|
46
43
|
# attr_reader :name
|
47
44
|
#
|
48
|
-
#
|
45
|
+
# result_attr :message
|
49
46
|
#
|
50
47
|
# def call
|
51
48
|
# success!(message: "Hello #{name}")
|
@@ -55,8 +52,11 @@ module Operatic
|
|
55
52
|
# result = SayHello.call(name: 'Dave')
|
56
53
|
# result.success? # => true
|
57
54
|
# result.message # => "Hello Dave"
|
58
|
-
|
59
|
-
|
55
|
+
#
|
56
|
+
# @param attrs [Array<Symbol>] a list of convenience data accessors to
|
57
|
+
# define on the {Result}.
|
58
|
+
def result_attr(*attrs)
|
59
|
+
@result_class = Result.generate(*attrs)
|
60
60
|
end
|
61
61
|
|
62
62
|
def result_class
|
@@ -64,22 +64,16 @@ module Operatic
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
|
68
|
-
#
|
69
|
-
# @return [Result]
|
70
|
-
attr_reader :result
|
71
|
-
|
72
|
-
def initialize(attrs = nil)
|
73
|
-
@result = self.class.result_class.new
|
74
|
-
|
67
|
+
def initialize(**attrs)
|
75
68
|
attrs.each do |key, value|
|
76
69
|
instance_variable_set("@#{key}", value)
|
77
|
-
end
|
70
|
+
end
|
78
71
|
end
|
79
72
|
|
80
73
|
# Override this method with your implementation. Use {#success!} or
|
81
74
|
# {#failure!} methods to communicate the {#result}'s status and to attach
|
82
|
-
# data it. Define convenience result accessors with
|
75
|
+
# data to it. Define convenience result accessors with
|
76
|
+
# {ClassMethods#result_attr}.
|
83
77
|
#
|
84
78
|
# @example
|
85
79
|
# class SayHello
|
@@ -87,6 +81,8 @@ module Operatic
|
|
87
81
|
#
|
88
82
|
# attr_reader :name
|
89
83
|
#
|
84
|
+
# result_attr :message
|
85
|
+
#
|
90
86
|
# def call
|
91
87
|
# return failure! unless name
|
92
88
|
# success!(message: "Hello #{name}")
|
@@ -94,23 +90,34 @@ module Operatic
|
|
94
90
|
# end
|
95
91
|
#
|
96
92
|
# result = SayHello.call(name: 'Dave')
|
93
|
+
# result.failure? # => false
|
97
94
|
# result.success? # => true
|
95
|
+
# result.message # => "Hello Dave"
|
98
96
|
# result.to_h # => {:message=>"Hello Dave"}
|
99
97
|
#
|
100
98
|
# result = SayHello.call
|
101
99
|
# result.failure? # => true
|
102
100
|
# result.success? # => false
|
101
|
+
# result.message # => nil
|
103
102
|
# result.to_h # => {}
|
104
103
|
def call
|
105
104
|
end
|
106
105
|
|
107
106
|
# Convenience shortcut to the operation's {Result#failure!}.
|
108
|
-
def failure!(data
|
109
|
-
result.failure!(data)
|
107
|
+
def failure!(**data)
|
108
|
+
result.failure!(**data)
|
109
|
+
end
|
110
|
+
|
111
|
+
# An instance of {Result} or a subclass generated by
|
112
|
+
# {ClassMethods#result_attr}.
|
113
|
+
#
|
114
|
+
# @return [Result]
|
115
|
+
def result
|
116
|
+
@result ||= self.class.result_class.new
|
110
117
|
end
|
111
118
|
|
112
119
|
# Convenience shortcut to the operation's {Result#success!}.
|
113
|
-
def success!(data
|
114
|
-
result.success!(data)
|
120
|
+
def success!(**data)
|
121
|
+
result.success!(**data)
|
115
122
|
end
|
116
123
|
end
|
data/operatic.gemspec
CHANGED
@@ -31,6 +31,6 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.require_paths = ['lib']
|
32
32
|
|
33
33
|
spec.add_development_dependency 'bundler'
|
34
|
-
spec.add_development_dependency 'rake'
|
34
|
+
spec.add_development_dependency 'rake'
|
35
35
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
36
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Pickles
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,7 +82,7 @@ metadata:
|
|
82
82
|
changelog_uri: https://github.com/benpickles/operatic/blob/master/CHANGELOG.md
|
83
83
|
documentation_uri: https://rubydoc.info/gems/operatic
|
84
84
|
source_code_uri: https://github.com/benpickles/operatic
|
85
|
-
post_install_message:
|
85
|
+
post_install_message:
|
86
86
|
rdoc_options: []
|
87
87
|
require_paths:
|
88
88
|
- lib
|
@@ -97,8 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
rubygems_version: 3.
|
101
|
-
signing_key:
|
100
|
+
rubygems_version: 3.3.7
|
101
|
+
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: A minimal standard interface for your operations
|
104
104
|
test_files: []
|