prezzo 0.4.1 → 0.5.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/README.md +78 -0
- data/lib/prezzo/explainable.rb +24 -16
- data/lib/prezzo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af6eff6786cc37bdb7e4cca103f0eee8a7e2c5fa
|
4
|
+
data.tar.gz: fdbf73bbfe95b5328feb1b84db4c90db24b54b8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/prezzo/explainable.rb
CHANGED
@@ -7,27 +7,35 @@ module Prezzo
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassMethods
|
10
|
-
def explain_with(*component_names)
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
17
|
-
|
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
|
-
|
20
|
+
def explain
|
21
|
+
explained_components = self.class.explained_components || {}
|
27
22
|
|
28
|
-
|
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
|
data/lib/prezzo/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2017-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hanami-validations
|