much-result 0.1.0 → 0.1.1

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: f4805f13dbc5498186070c28c422564440a511d0b9e4af9f695802b969e3f7da
4
- data.tar.gz: 811cb65cfb9bb197329e06b44ce7511daab25adc9bbdbcd1610d307c2f1b4cb3
3
+ metadata.gz: 50f359b93392a57fb5a5706b9b127b6683b4d9bbff35414d6dd2f88e13c3fe92
4
+ data.tar.gz: 85bd060061eb19a84d5fbb44e64ff4512f1d0cafece8049880ffa76933314fd8
5
5
  SHA512:
6
- metadata.gz: 2e3a53796159c10e8b8e05ab1afd862cdbc77bc0eb729d3874808f731ae8f82a69f71479fb3664dffc3cb69c0bc4a815a9e29f1c1ed5c4ba1f71b584bdd29377
7
- data.tar.gz: 77b2ac454e76ba063bc3ff2812a063caafe776d42e0875f111fa17868d58a1716f0ced1e423bb045f723250e5f6e72f11f3bc5acd2cd3db89cff3b4e77e704ba
6
+ metadata.gz: 704dc5d29a3d8000ec35fe54b92055e2e2e491b0c30169bc1218476fac0a8c7e5ba5e9d308f9afa06b3be72b636333250726c379340db241d59fe5e795c25987
7
+ data.tar.gz: afd00a2cd6dd19af02af0c05d280434057440b6a5c4f5de713936fcc3d185abb11d22407d3ee68afcd4a97a43ab381a97e0eafc77730ec005a6479a50dab45c4
data/README.md CHANGED
@@ -210,6 +210,18 @@ result.get_for_all_failure_results(:description)
210
210
 
211
211
  Note: MuchResult::Transactions are designed to delegate to their MuchResult. You can interact with a MuchResult::Transaction as if it were a MuchResult.
212
212
 
213
+ You can configure a default transaction receiver (e.g. `ActiveRecord::Base`) in an initializer. Doing so means if no receiver is passed to `MuchResult.transaction`, the default receiver will be used:
214
+
215
+ ```ruby
216
+ # In an initializer or configuration script:
217
+ MuchResult.default_transaction_receiver = ActiveRecord::Base
218
+
219
+ # Since no receiver is passed, ActiveRecord::Base will be used:
220
+ MuchResult.transaction do |transaction|
221
+ # ...
222
+ end
223
+ ```
224
+
213
225
  ## Installation
214
226
 
215
227
  Add this line to your application's Gemfile:
@@ -35,8 +35,28 @@ class MuchResult
35
35
  }
36
36
  end
37
37
 
38
- def self.transaction(receiver, backtrace: caller, **kargs, &block)
39
- MuchResult::Transaction.call(receiver, backtrace: backtrace, **kargs, &block)
38
+ def self.transaction(receiver = nil, backtrace: caller, **kargs, &block)
39
+ if (transaction_receiver = receiver || default_transaction_receiver).nil?
40
+ raise(
41
+ ArgumentError,
42
+ "no receiver given and no default_transaction_receiver configured."
43
+ )
44
+ end
45
+
46
+ MuchResult::Transaction.(
47
+ receiver || default_transaction_receiver,
48
+ backtrace: backtrace,
49
+ **kargs,
50
+ &block
51
+ )
52
+ end
53
+
54
+ def self.default_transaction_receiver
55
+ @default_transaction_receiver
56
+ end
57
+
58
+ def self.default_transaction_receiver=(receiver)
59
+ @default_transaction_receiver = receiver
40
60
  end
41
61
 
42
62
  attr_reader :sub_results, :description, :backtrace
@@ -11,6 +11,8 @@ class MuchResult::Transaction
11
11
  end
12
12
 
13
13
  def initialize(receiver, **result_kargs)
14
+ raise(ArgumentError, "`receiver` can't be nil.") if receiver.nil?
15
+
14
16
  @receiver = receiver
15
17
  @result_kargs = result_kargs
16
18
 
@@ -1,3 +1,3 @@
1
1
  class MuchResult
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -14,6 +14,7 @@ class MuchResult
14
14
  let(:value1) { Factory.value }
15
15
 
16
16
  should have_imeths :success, :failure, :for, :tap, :transaction
17
+ should have_accessors :default_transaction_receiver
17
18
 
18
19
  should "build success instances" do
19
20
  result = subject.success
@@ -113,6 +114,35 @@ class MuchResult
113
114
 
114
115
  assert_that(result.sub_results.size).equals(1)
115
116
  end
117
+
118
+ should "configure default transaction receivers" do
119
+ MuchStub.on_call(MuchResult::Transaction, :call) { |call|
120
+ @transaction_call = call
121
+ }
122
+
123
+ receiver1 = Factory.transaction_receiver
124
+ kargs1 = {
125
+ backtrace: Factory.backtrace,
126
+ value: value1
127
+ }
128
+ block1 = -> {}
129
+
130
+ assert_that(subject.default_transaction_receiver).is_nil
131
+ assert_that(-> {
132
+ subject.transaction(**kargs1, &block1)
133
+ }).raises(ArgumentError)
134
+
135
+ subject.default_transaction_receiver = receiver1
136
+ subject.transaction(**kargs1, &block1)
137
+
138
+ assert_that(subject.default_transaction_receiver).equals(receiver1)
139
+ assert_that(@transaction_call.pargs).equals([receiver1])
140
+ assert_that(@transaction_call.kargs).equals(kargs1)
141
+ assert_that(@transaction_call.block).equals(block1)
142
+
143
+ subject.default_transaction_receiver = nil
144
+ assert_that(subject.default_transaction_receiver).is_nil
145
+ end
116
146
  end
117
147
 
118
148
  class InitTests < UnitTests
@@ -45,6 +45,12 @@ class MuchResult::Transaction
45
45
 
46
46
  should have_imeths :result, :call, :rollback, :halt
47
47
 
48
+ should "complain if given a nil receiver" do
49
+ assert_that(-> {
50
+ unit_class.new(nil, **kargs1)
51
+ }).raises(ArgumentError)
52
+ end
53
+
48
54
  should "know its result" do
49
55
  assert_that(subject.result).is_instance_of(MuchResult)
50
56
  assert_that(subject.result.value).equals(value1)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: much-result
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-09-09 00:00:00.000000000 Z
12
+ date: 2020-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: assert