Skip to content

Commit d48d9ad

Browse files
committed
Added :update method to set multiple values within a single call.
1 parent a74c6a7 commit d48d9ad

9 files changed

+389
-279
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 1.4.0
4+
- New method `:update` added.
5+
- The :update method accepts data to be added in the format `key: value`.
6+
- New data is created and existing data is updated.
7+
- E.g. `result.update( success: true, name: 'Tester', time: Time.now )`
8+
39
## 1.3.0
410
- When recording an exception:
511
- Unless perviously defined, the :error_message attribute is automatically set with the exception's error message.

Gemfile.lock

+38-36
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,80 @@
11
PATH
22
remote: .
33
specs:
4-
result_vault (1.3.0)
4+
result_vault (1.4.0)
55

66
GEM
77
remote: https://rubygems.org/
88
specs:
9-
amazing_print (1.5.0)
9+
amazing_print (1.6.0)
1010
ast (2.4.2)
11-
debug (1.9.1)
11+
debug (1.9.2)
1212
irb (~> 1.10)
1313
reline (>= 0.3.8)
14-
diff-lcs (1.5.0)
15-
docile (1.4.0)
14+
diff-lcs (1.5.1)
15+
docile (1.4.1)
1616
io-console (0.7.2)
17-
irb (1.11.1)
18-
rdoc
17+
irb (1.14.0)
18+
rdoc (>= 4.0.0)
1919
reline (>= 0.4.2)
20-
json (2.7.1)
20+
json (2.7.2)
2121
language_server-protocol (3.17.0.3)
22-
parallel (1.24.0)
23-
parser (3.3.0.5)
22+
parallel (1.26.2)
23+
parser (3.3.4.2)
2424
ast (~> 2.4.1)
2525
racc
2626
psych (5.1.2)
2727
stringio
28-
racc (1.7.3)
28+
racc (1.8.1)
2929
rainbow (3.1.1)
30-
rake (13.1.0)
31-
rdoc (6.6.2)
30+
rake (13.2.1)
31+
rdoc (6.7.0)
3232
psych (>= 4.0.0)
33-
regexp_parser (2.9.0)
34-
reline (0.4.2)
33+
regexp_parser (2.9.2)
34+
reline (0.5.9)
3535
io-console (~> 0.5)
36-
rexml (3.2.6)
37-
rspec (3.12.0)
38-
rspec-core (~> 3.12.0)
39-
rspec-expectations (~> 3.12.0)
40-
rspec-mocks (~> 3.12.0)
41-
rspec-core (3.12.2)
42-
rspec-support (~> 3.12.0)
43-
rspec-expectations (3.12.3)
36+
rexml (3.3.5)
37+
strscan
38+
rspec (3.13.0)
39+
rspec-core (~> 3.13.0)
40+
rspec-expectations (~> 3.13.0)
41+
rspec-mocks (~> 3.13.0)
42+
rspec-core (3.13.0)
43+
rspec-support (~> 3.13.0)
44+
rspec-expectations (3.13.1)
4445
diff-lcs (>= 1.2.0, < 2.0)
45-
rspec-support (~> 3.12.0)
46-
rspec-mocks (3.12.6)
46+
rspec-support (~> 3.13.0)
47+
rspec-mocks (3.13.1)
4748
diff-lcs (>= 1.2.0, < 2.0)
48-
rspec-support (~> 3.12.0)
49-
rspec-support (3.12.1)
50-
rubocop (1.60.1)
49+
rspec-support (~> 3.13.0)
50+
rspec-support (3.13.1)
51+
rubocop (1.65.1)
5152
json (~> 2.3)
5253
language_server-protocol (>= 3.17.0)
5354
parallel (~> 1.10)
5455
parser (>= 3.3.0.2)
5556
rainbow (>= 2.2.2, < 4.0)
56-
regexp_parser (>= 1.8, < 3.0)
57+
regexp_parser (>= 2.4, < 3.0)
5758
rexml (>= 3.2.5, < 4.0)
58-
rubocop-ast (>= 1.30.0, < 2.0)
59+
rubocop-ast (>= 1.31.1, < 2.0)
5960
ruby-progressbar (~> 1.7)
6061
unicode-display_width (>= 2.4.0, < 3.0)
61-
rubocop-ast (1.30.0)
62-
parser (>= 3.2.1.0)
62+
rubocop-ast (1.32.0)
63+
parser (>= 3.3.1.0)
6364
ruby-progressbar (1.13.0)
6465
simplecov (0.22.0)
6566
docile (~> 1.1)
6667
simplecov-html (~> 0.11)
6768
simplecov_json_formatter (~> 0.1)
6869
simplecov-html (0.12.3)
6970
simplecov_json_formatter (0.1.4)
70-
stringio (3.1.0)
71+
stringio (3.1.1)
72+
strscan (3.1.0)
7173
unicode-display_width (2.5.0)
72-
yard (0.9.34)
74+
yard (0.9.36)
7375

7476
PLATFORMS
75-
arm64-darwin-22
77+
arm64-darwin-23
7678
ruby
7779
x86_64-linux
7880

@@ -87,4 +89,4 @@ DEPENDENCIES
8789
yard
8890

8991
BUNDLED WITH
90-
2.5.5
92+
2.5.17

README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- [:exception](#exception)
4545
- [:error_message](#error_message)
4646
- [:data](#data)
47+
- [:update](#update)
4748
- [freezing results](#freezing)
4849
- [change log](CHANGELOG.md)
4950
- [code of conduct](#code-of-conduct)
@@ -195,6 +196,7 @@ result.user_name #=> 'Test User'
195196
- [:exception](#exception)
196197
- [:error_message](#error_message)
197198
- [:data](#data)
199+
- [:update](#update)
198200

199201

200202
There are five methods pre-defined to help developers easily determine
@@ -309,7 +311,32 @@ result = average(num_array)
309311
result.data #=> {:total=>15, :size=>5, :average=>3, :success=>true, :status=>nil, :error_message=>""}
310312
```
311313

312-
> **Note:** <br/>`:data` cannot be set directly.
314+
> **Note:** <br/>Use `:update` to add or update data.
315+
316+
317+
318+
<a name='update'></a>
319+
### :update
320+
321+
`:update` sets new or updates existing data values:
322+
323+
```ruby
324+
result = ResultVault.new(age: 9.75)
325+
result.success? #=> false
326+
result.age #=> 9.75
327+
result.name #=> Undefined method `name'
328+
329+
result.update(success: true, age: 23, name: 'Tester')
330+
331+
result.success? #=> true
332+
result.age #=> 23
333+
result.name #=> 'Tester'
334+
end
335+
336+
num_array = [1,2,3,4,5]
337+
result = average(num_array)
338+
result.data #=> {:total=>15, :size=>5, :average=>3, :success=>true, :status=>nil, :error_message=>""}
339+
```
313340

314341
---
315342

lib/result_vault.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,29 @@ def error_message=(err_msg)
110110
end
111111

112112

113+
##
114+
# Set multiple data in a single call
115+
#
116+
# @param [Hash] args
117+
#
118+
def update(**args)
119+
args.each do |k, v|
120+
if k.is_a?(Symbol)
121+
send("#{k.downcase}=", v)
122+
else
123+
fail ArgumentError, ":update argument key not a Symbol: '#{k}'."
124+
end
125+
end
126+
127+
return self
128+
end
129+
130+
113131
##
114132
# Raise an exception if the user is setting data directly
115133
#
116134
def data=(*)
117-
fail ArgumentError, ":data is a reserved keyword and may not be set directly."
135+
fail ArgumentError, "use :update to set or update results."
118136
end
119137

120138

lib/result_vault/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
class ResultVault
4-
VERSION = '1.3.0'
4+
VERSION = '1.4.0'
55
end

spec/getter_methods_spec.rb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
RSpec.describe ResultVault, type: :model do
2+
3+
describe 'getter methods' do
4+
let(:result) { ResultVault.new(test_1: 'One', test_2: 'Two') }
5+
6+
%i[success ok good pass passed succeeded].each do |name|
7+
context ":#{name}" do
8+
it 'returns the value of :success?' do
9+
res = ResultVault.new(success: true)
10+
expect(res.send("#{name}?")).to be_truthy
11+
end
12+
end
13+
end # each name
14+
15+
describe ':data' do
16+
it 'returns a hash of the result data' do
17+
expect(result.data).to be_a Hash
18+
expect(result.data.keys).to include :test_1
19+
expect(result.data.keys).to include :test_2
20+
expect(result.data[:test_1]).to eq 'One'
21+
expect(result.data[:test_2]).to eq 'Two'
22+
end
23+
24+
it 'freezes the returned hash' do
25+
expect(result.data).to be_frozen
26+
end
27+
end # ":data"
28+
29+
describe ':status' do
30+
it 'returns the status' do
31+
expect(result.status).to be_nil
32+
result.status = :test_status
33+
expect(result.status).to eq :test_status
34+
end
35+
end # :status
36+
37+
describe ':exception' do
38+
it 'returns the exception value' do
39+
result.exception = ArgumentError.new
40+
expect(result.exception).to be_a Exception
41+
end
42+
end # :exception
43+
44+
describe ':error_message' do
45+
it 'returns the error message' do
46+
expect(result.error_message).to be_empty
47+
result.error_message = 'Test Message'
48+
expect(result.error_message).to eq 'Test Message'
49+
end
50+
end # :error_message
51+
end
52+
end

0 commit comments

Comments
 (0)