operatic 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d23afc6809c6d2b335d1db3747eeaa96e931f6a8e3dd51c8cd52261512b78bd4
4
- data.tar.gz: f21c35d239aca9518215252494bc1aeb8572750b56e30a4c94ee6aa0e2215aa0
3
+ metadata.gz: aeb2128119d4f471de6cd2ef4bc70e7d2a5ad73bf3a21867cbd8ead4d6e2a387
4
+ data.tar.gz: 53ba7951114c31a60a360f95c801b4900603daf010e9f8d4f8aeded22ea657db
5
5
  SHA512:
6
- metadata.gz: 010f2b26e6129b0fd7b6ac5c0c1499f1fadb9a0b31027b3ed4334cb54e2a5ecdbf4710e092c64457f6f10f7d97de016887bf7c356b2ffe8711be534e67214eee
7
- data.tar.gz: 5e32262f6568ff446897a86840cb1f8bb1a3b19ca0a89fa05ffd7c32ef9f388c8e81862a6b07ea070488d44f3550893d316833e416ec0e1d3509448edf4faec2
6
+ metadata.gz: 2f50a0a06fc96f281822f2ccc5696e2608d387b7720c49bf861f735a566e6d717fe90c3d57aed36ffcd09ddfebe9501c27d8e8cc7abfb7c1fb28c9e349a25972
7
+ data.tar.gz: f47a4fb2db7a8facc43235ae6bbd9d8c0f86e3a55c998a73666e360caee833227c1385a64b4c6b47212c41ecd640832f3248fc16f68db09745a75cf1ecce5c1f
@@ -12,6 +12,7 @@ jobs:
12
12
  - '2.6'
13
13
  - '2.7'
14
14
  - '3.0'
15
+ - '3.1'
15
16
  name: Ruby ${{ matrix.ruby }} RSpec
16
17
  steps:
17
18
  - uses: actions/checkout@v2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## Version 0.4.0 - 2022-05-25
4
+
5
+ - Switch to keyword arguments. <https://github.com/benpickles/operatic/pull/8>
6
+
3
7
  ## Version 0.3.1 - 2020-01-05
4
8
 
5
9
  - Less specific Rake dependency. <https://github.com/benpickles/operatic/pull/6>
@@ -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 accessors to the result's data.
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 freeze the
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*: After calling this method calling {#success!} or {#failure!}
31
- # again will raise a +FrozenError+.
32
- #
33
- # @param data [Hash<Symbol, anything>] an optional hash of data to attach
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 freeze the
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 result defaults to being 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*: After calling this method calling {#success!} or {#failure!}
58
- # again will raise a +FrozenError+.
59
- #
60
- # @param data [Hash<Symbol, anything>] an optional hash of data to attach
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
@@ -1,3 +1,3 @@
1
1
  module Operatic
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
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+ and calls {Operatic#call}
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
- # @param attrs [Hash<Symbol, anything>] an optional hash of key/values to
18
- # to the result. The class must have corresponding +attr_reader+s
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 = nil)
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
- def result(*args)
59
- @result_class = Result.generate(*args)
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 = nil)
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 if attrs
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 = nil)
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 = nil)
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.3.1
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: 2021-01-05 00:00:00.000000000 Z
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.2.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