operatic 0.3.1 → 0.4.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/.github/workflows/ruby.yml +1 -0
- data/CHANGELOG.md +4 -0
- data/lib/operatic/result.rb +16 -22
- data/lib/operatic/version.rb +1 -1
- data/lib/operatic.rb +24 -19
- 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: aeb2128119d4f471de6cd2ef4bc70e7d2a5ad73bf3a21867cbd8ead4d6e2a387
|
4
|
+
data.tar.gz: 53ba7951114c31a60a360f95c801b4900603daf010e9f8d4f8aeded22ea657db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f50a0a06fc96f281822f2ccc5696e2608d387b7720c49bf861f735a566e6d717fe90c3d57aed36ffcd09ddfebe9501c27d8e8cc7abfb7c1fb28c9e349a25972
|
7
|
+
data.tar.gz: f47a4fb2db7a8facc43235ae6bbd9d8c0f86e3a55c998a73666e360caee833227c1385a64b4c6b47212c41ecd640832f3248fc16f68db09745a75cf1ecce5c1f
|
data/.github/workflows/ruby.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/lib/operatic/result.rb
CHANGED
@@ -4,7 +4,7 @@ module Operatic
|
|
4
4
|
# wouldn't normally be called directly, see {ClassMethods#result} 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,13 @@ module Operatic
|
|
24
24
|
@success = true
|
25
25
|
end
|
26
26
|
|
27
|
-
# Mark the result as a failure, optionally attach data, and
|
28
|
-
# object so it cannot be modified further.
|
27
|
+
# Mark the result as a failure, optionally attach +data+ via kwargs, and
|
28
|
+
# freeze the object so it cannot be modified further.
|
29
29
|
#
|
30
|
-
# *Note*:
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
# to the result.
|
35
|
-
def failure!(data = nil)
|
36
|
-
set_data(data) if data
|
30
|
+
# *Note*: Calling {#success!} or {#failure!} more than once will raise a
|
31
|
+
# +FrozenError+.
|
32
|
+
def failure!(**data)
|
33
|
+
set_data(**data)
|
37
34
|
@success = false
|
38
35
|
freeze
|
39
36
|
end
|
@@ -47,20 +44,17 @@ module Operatic
|
|
47
44
|
super
|
48
45
|
end
|
49
46
|
|
50
|
-
# Mark the result as a success, optionally attach data, and
|
51
|
-
# object so it cannot be modified further.
|
47
|
+
# Mark the result as a success, optionally attach +data+ via kwargs, and
|
48
|
+
# freeze the object so it cannot be modified further.
|
52
49
|
#
|
53
|
-
# Calling this is not strictly necessary as a
|
50
|
+
# Calling this is not strictly necessary as a +Result+ defaults to being a
|
54
51
|
# success, but it's a convenient means of attaching data and of indicating
|
55
52
|
# intent in the consuming code.
|
56
53
|
#
|
57
|
-
# *Note*:
|
58
|
-
#
|
59
|
-
|
60
|
-
|
61
|
-
# to the result.
|
62
|
-
def success!(data = nil)
|
63
|
-
set_data(data) if data
|
54
|
+
# *Note*: Calling {#success!} or {#failure!} more than once will raise a
|
55
|
+
# +FrozenError+.
|
56
|
+
def success!(**data)
|
57
|
+
set_data(**data)
|
64
58
|
@success = true
|
65
59
|
freeze
|
66
60
|
end
|
@@ -72,13 +66,13 @@ module Operatic
|
|
72
66
|
# Returns the full (frozen) hash of data attached to the result via
|
73
67
|
# {#success!}, {#failure!}, or convenience accessors added with {.generate}.
|
74
68
|
#
|
75
|
-
# @return [Hash]
|
69
|
+
# @return [Hash<Symbol, anything>]
|
76
70
|
def to_h
|
77
71
|
@data
|
78
72
|
end
|
79
73
|
|
80
74
|
private
|
81
|
-
def set_data(data)
|
75
|
+
def set_data(**data)
|
82
76
|
data.each do |key, value|
|
83
77
|
@data[key] = value
|
84
78
|
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
|
@@ -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(*attrs)
|
59
|
+
@result_class = Result.generate(*attrs)
|
60
60
|
end
|
61
61
|
|
62
62
|
def result_class
|
@@ -69,17 +69,17 @@ module Operatic
|
|
69
69
|
# @return [Result]
|
70
70
|
attr_reader :result
|
71
71
|
|
72
|
-
def initialize(attrs
|
72
|
+
def initialize(**attrs)
|
73
73
|
@result = self.class.result_class.new
|
74
74
|
|
75
75
|
attrs.each do |key, value|
|
76
76
|
instance_variable_set("@#{key}", value)
|
77
|
-
end
|
77
|
+
end
|
78
78
|
end
|
79
79
|
|
80
80
|
# Override this method with your implementation. Use {#success!} or
|
81
81
|
# {#failure!} methods to communicate the {#result}'s status and to attach
|
82
|
-
# data it. Define convenience result accessors with {ClassMethods#result}.
|
82
|
+
# data to it. Define convenience result accessors with {ClassMethods#result}.
|
83
83
|
#
|
84
84
|
# @example
|
85
85
|
# class SayHello
|
@@ -87,6 +87,8 @@ module Operatic
|
|
87
87
|
#
|
88
88
|
# attr_reader :name
|
89
89
|
#
|
90
|
+
# result :message
|
91
|
+
#
|
90
92
|
# def call
|
91
93
|
# return failure! unless name
|
92
94
|
# success!(message: "Hello #{name}")
|
@@ -94,23 +96,26 @@ module Operatic
|
|
94
96
|
# end
|
95
97
|
#
|
96
98
|
# result = SayHello.call(name: 'Dave')
|
99
|
+
# result.failure? # => false
|
97
100
|
# result.success? # => true
|
101
|
+
# result.message # => "Hello Dave"
|
98
102
|
# result.to_h # => {:message=>"Hello Dave"}
|
99
103
|
#
|
100
104
|
# result = SayHello.call
|
101
105
|
# result.failure? # => true
|
102
106
|
# result.success? # => false
|
107
|
+
# result.message # => nil
|
103
108
|
# result.to_h # => {}
|
104
109
|
def call
|
105
110
|
end
|
106
111
|
|
107
112
|
# Convenience shortcut to the operation's {Result#failure!}.
|
108
|
-
def failure!(data
|
109
|
-
result.failure!(data)
|
113
|
+
def failure!(**data)
|
114
|
+
result.failure!(**data)
|
110
115
|
end
|
111
116
|
|
112
117
|
# Convenience shortcut to the operation's {Result#success!}.
|
113
|
-
def success!(data
|
114
|
-
result.success!(data)
|
118
|
+
def success!(**data)
|
119
|
+
result.success!(**data)
|
115
120
|
end
|
116
121
|
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.4.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:
|
11
|
+
date: 2022-05-25 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.1.6
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: A minimal standard interface for your operations
|