operatic 0.4.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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +10 -8
- data/lib/operatic/result.rb +11 -1
- data/lib/operatic/version.rb +1 -1
- data/lib/operatic.rb +13 -11
- metadata +3 -3
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/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
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
|
+
|
3
9
|
## Version 0.4.0 - 2022-05-25
|
4
10
|
|
5
11
|
- Switch to keyword arguments. <https://github.com/benpickles/operatic/pull/8>
|
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,7 +1,7 @@
|
|
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
7
|
# @param attrs [Array<Symbol>] a list of convenience data accessors.
|
@@ -24,6 +24,16 @@ module Operatic
|
|
24
24
|
@success = true
|
25
25
|
end
|
26
26
|
|
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
|
+
|
27
37
|
# Mark the result as a failure, optionally attach +data+ via kwargs, and
|
28
38
|
# freeze the object so it cannot be modified further.
|
29
39
|
#
|
data/lib/operatic/version.rb
CHANGED
data/lib/operatic.rb
CHANGED
@@ -42,7 +42,7 @@ module Operatic
|
|
42
42
|
#
|
43
43
|
# attr_reader :name
|
44
44
|
#
|
45
|
-
#
|
45
|
+
# result_attr :message
|
46
46
|
#
|
47
47
|
# def call
|
48
48
|
# success!(message: "Hello #{name}")
|
@@ -55,7 +55,7 @@ module Operatic
|
|
55
55
|
#
|
56
56
|
# @param attrs [Array<Symbol>] a list of convenience data accessors to
|
57
57
|
# define on the {Result}.
|
58
|
-
def
|
58
|
+
def result_attr(*attrs)
|
59
59
|
@result_class = Result.generate(*attrs)
|
60
60
|
end
|
61
61
|
|
@@ -64,14 +64,7 @@ module Operatic
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
# An instance of {Result} or a subclass generated by {ClassMethods#result}.
|
68
|
-
#
|
69
|
-
# @return [Result]
|
70
|
-
attr_reader :result
|
71
|
-
|
72
67
|
def initialize(**attrs)
|
73
|
-
@result = self.class.result_class.new
|
74
|
-
|
75
68
|
attrs.each do |key, value|
|
76
69
|
instance_variable_set("@#{key}", value)
|
77
70
|
end
|
@@ -79,7 +72,8 @@ module Operatic
|
|
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 to 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,7 +81,7 @@ module Operatic
|
|
87
81
|
#
|
88
82
|
# attr_reader :name
|
89
83
|
#
|
90
|
-
#
|
84
|
+
# result_attr :message
|
91
85
|
#
|
92
86
|
# def call
|
93
87
|
# return failure! unless name
|
@@ -114,6 +108,14 @@ module Operatic
|
|
114
108
|
result.failure!(**data)
|
115
109
|
end
|
116
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
|
117
|
+
end
|
118
|
+
|
117
119
|
# Convenience shortcut to the operation's {Result#success!}.
|
118
120
|
def success!(**data)
|
119
121
|
result.success!(**data)
|
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
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
rubygems_version: 3.
|
100
|
+
rubygems_version: 3.3.7
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: A minimal standard interface for your operations
|