prezzo 0.4.1 → 0.5.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
  SHA1:
3
- metadata.gz: 11bbbefd55c61e98dbbc825bd62914cf8c5db61e
4
- data.tar.gz: 7e8964b2729124602136631ee8c7e7ae05880632
3
+ metadata.gz: af6eff6786cc37bdb7e4cca103f0eee8a7e2c5fa
4
+ data.tar.gz: fdbf73bbfe95b5328feb1b84db4c90db24b54b8c
5
5
  SHA512:
6
- metadata.gz: 9766a2ecc37debfe6186f6ee7b5101458261391ea9b7ca0ceba6c4648bb0c2d51cae70db6d30752cf3506ed9ac6ca99c3d491b0085dccb048a1df44fb4d02e53
7
- data.tar.gz: a106a37ad16a882536acad010229dfaa01e2b6c764173b90ca35458f742294d29ff98650693f8872f73562ec824a088c5c361283ccb259823704d549ceff564e
6
+ metadata.gz: 334ac63972f842fec450af16c3cbddd9037a283549e69c47ddbc20f7c26b592579c7dd9c7e976d87861c152c0b8955938eb78fb2e0b7d6145ec20c598e219f54
7
+ data.tar.gz: d1b24522f93fbd58093317165cfbf3f3e1dfc3ae7be0b2010899936613240f0c97b9b423dc63caa256fcaf2b6fb49bbc8825d5ac33844401c695e9e093f6422e
data/README.md CHANGED
@@ -153,6 +153,84 @@ Uber::RidePriceCalculator.new(context).explain
153
153
  #=> { total: 25.6, components: { base_fare: 4.3, price_per_distance: 21.3 } }
154
154
  ```
155
155
 
156
+ #### Multiline `explain_with`
157
+
158
+ `explain_with` can be splitted into several lines.
159
+
160
+ ```ruby
161
+ class RidePriceCalculator
162
+ include Prezzo::Explainable
163
+
164
+ explain_with :base_fare
165
+ explain_with :price_per_distance
166
+ end
167
+ ```
168
+
169
+ #### ` explain_with` with the `resursive: false` option
170
+
171
+ ```ruby
172
+ class FooCalculator
173
+ include Prezzo::Calculator
174
+ include Prezzo::Explainable
175
+
176
+ explain_with :bar, :baz
177
+
178
+ def calculate
179
+ bar + baz
180
+ end
181
+
182
+ def bar
183
+ 10
184
+ end
185
+
186
+ def baz
187
+ 20
188
+ end
189
+ end
190
+
191
+ class QuxCalculator
192
+ include Prezzo::Calculator
193
+ include Prezzo::Composable
194
+ include Prezzo::Explainable
195
+
196
+ composed_by foo: FooCalculator
197
+
198
+ explain_with :foo, resursive: false
199
+
200
+ def calculate
201
+ foo + 5
202
+ end
203
+ end
204
+ ```
205
+
206
+ `QuxCalculator#explain` now produces
207
+
208
+ ```ruby
209
+ {
210
+ total: 35,
211
+ components: {
212
+ foo: 30
213
+ }
214
+ }
215
+ ```
216
+
217
+ but not
218
+
219
+ ```ruby
220
+ {
221
+ total: 35,
222
+ components: {
223
+ foo: {
224
+ total: 30,
225
+ components: {
226
+ bar: 10,
227
+ baz: 20
228
+ }
229
+ }
230
+ }
231
+ }
232
+ ```
233
+
156
234
  Check the full [Uber pricing](/spec/integration/uber_pricing_spec.rb) for more complete example with many calculators and factors.
157
235
 
158
236
  ## Development
@@ -7,27 +7,35 @@ module Prezzo
7
7
  end
8
8
 
9
9
  module ClassMethods
10
- def explain_with(*component_names)
11
- define_method(:explain) do
12
- explanation = {
13
- total: calculate,
14
- }
10
+ def explain_with(*component_names, **options)
11
+ @explained_components ||= {}
12
+ component_names.each do |component_name|
13
+ @explained_components[component_name] = options
14
+ end
15
+ end
15
16
 
16
- components = component_names.each_with_object({}) do |component, acc|
17
- value = send(component)
18
- if self.class.respond_to?(:components) && self.class.components.include?(component)
19
- value = cached_components[component]
20
- end
21
- value = value.explain if value.respond_to?(:explain)
22
- value = value.calculate if value.respond_to?(:calculate)
23
- acc[component] = value
24
- end
17
+ attr_reader :explained_components
18
+ end
25
19
 
26
- explanation[:components] = components unless components.empty?
20
+ def explain
21
+ explained_components = self.class.explained_components || {}
27
22
 
28
- explanation
23
+ explanation = {
24
+ total: calculate,
25
+ components: {},
26
+ }
27
+
28
+ explained_components.each do |component, options|
29
+ value = send(component)
30
+ if self.class.respond_to?(:components) && self.class.components.include?(component)
31
+ value = cached_components[component]
29
32
  end
33
+ value = value.explain if value.respond_to?(:explain) && options.fetch(:resursive, true)
34
+ value = value.calculate if value.respond_to?(:calculate)
35
+ explanation[:components][component] = value
30
36
  end
37
+
38
+ explanation
31
39
  end
32
40
  end
33
41
  end
@@ -1,3 +1,3 @@
1
1
  module Prezzo
2
- VERSION = "0.4.1".freeze
2
+ VERSION = "0.5.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prezzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Boeira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-04 00:00:00.000000000 Z
11
+ date: 2017-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hanami-validations