price_waterfall 0.1.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 +7 -0
- data/CHANGELOG.md +186 -0
- data/LICENSE.txt +21 -0
- data/README.md +582 -0
- data/lib/price_waterfall/builder/arguments.rb +86 -0
- data/lib/price_waterfall/builder.rb +192 -0
- data/lib/price_waterfall/conditions/all.rb +18 -0
- data/lib/price_waterfall/conditions/any.rb +18 -0
- data/lib/price_waterfall/conditions/comparison.rb +142 -0
- data/lib/price_waterfall/conditions/compiled.rb +32 -0
- data/lib/price_waterfall/conditions/compiler.rb +182 -0
- data/lib/price_waterfall/conditions/custom.rb +59 -0
- data/lib/price_waterfall/conditions/group.rb +45 -0
- data/lib/price_waterfall/conditions/negation.rb +36 -0
- data/lib/price_waterfall/conditions/reason.rb +227 -0
- data/lib/price_waterfall/configuration_error.rb +8 -0
- data/lib/price_waterfall/entry.rb +111 -0
- data/lib/price_waterfall/error.rb +24 -0
- data/lib/price_waterfall/invalid_amount.rb +7 -0
- data/lib/price_waterfall/keys.rb +38 -0
- data/lib/price_waterfall/loader/order.rb +51 -0
- data/lib/price_waterfall/loader/problems.rb +72 -0
- data/lib/price_waterfall/loader/step_entry.rb +114 -0
- data/lib/price_waterfall/loader.rb +79 -0
- data/lib/price_waterfall/number.rb +149 -0
- data/lib/price_waterfall/percentage.rb +77 -0
- data/lib/price_waterfall/pipeline.rb +111 -0
- data/lib/price_waterfall/registered.rb +87 -0
- data/lib/price_waterfall/registry.rb +127 -0
- data/lib/price_waterfall/resolver.rb +148 -0
- data/lib/price_waterfall/result.rb +200 -0
- data/lib/price_waterfall/rounding.rb +119 -0
- data/lib/price_waterfall/run.rb +113 -0
- data/lib/price_waterfall/step_error.rb +9 -0
- data/lib/price_waterfall/step_name.rb +56 -0
- data/lib/price_waterfall/steps/amount.rb +50 -0
- data/lib/price_waterfall/steps/base.rb +40 -0
- data/lib/price_waterfall/steps/callable.rb +48 -0
- data/lib/price_waterfall/steps/cents.rb +35 -0
- data/lib/price_waterfall/steps/custom.rb +50 -0
- data/lib/price_waterfall/steps/override.rb +23 -0
- data/lib/price_waterfall/steps/percent.rb +17 -0
- data/lib/price_waterfall/steps/percentage_change.rb +26 -0
- data/lib/price_waterfall/steps/round.rb +46 -0
- data/lib/price_waterfall/steps/skipping.rb +43 -0
- data/lib/price_waterfall/steps/step.rb +200 -0
- data/lib/price_waterfall/steps/surcharge.rb +17 -0
- data/lib/price_waterfall/steps/tiered.rb +95 -0
- data/lib/price_waterfall/time_window.rb +93 -0
- data/lib/price_waterfall/timestamp.rb +58 -0
- data/lib/price_waterfall/user_code.rb +72 -0
- data/lib/price_waterfall/values/list.rb +28 -0
- data/lib/price_waterfall/values/literal.rb +28 -0
- data/lib/price_waterfall/values/path.rb +106 -0
- data/lib/price_waterfall/values/source.rb +44 -0
- data/lib/price_waterfall/version.rb +6 -0
- data/lib/price_waterfall.rb +126 -0
- metadata +103 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
# @api private
|
|
5
|
+
# One pricing of one item: the moment it prices at, the results of registered sources and
|
|
6
|
+
# operators, and how condition reasons are written.
|
|
7
|
+
#
|
|
8
|
+
# - **"Now" is read once.** Every time-limited step in a run sees the same moment: the +now:+
|
|
9
|
+
# given to the call, or else each clock is called at most once. Two steps that end at the same
|
|
10
|
+
# time can't see different sides of it, so a quote at midnight never gets half a promotion.
|
|
11
|
+
# - **Registered sources and operators run once per input within a step.** Their results are
|
|
12
|
+
# kept until the next step starts, so a skipped step's note doesn't call them again. Inputs are
|
|
13
|
+
# compared by identity, which is quick and never calls the inputs' own methods; results are
|
|
14
|
+
# dropped between steps because a step (a custom one) may change the item or context in place,
|
|
15
|
+
# and the next step must read what is there now.
|
|
16
|
+
#
|
|
17
|
+
# {Pipeline#call} and {Steps::Step#call} start a run and set it for the current fiber while they
|
|
18
|
+
# run, so the steps, sources and conditions inside find it with {.current} and keep their
|
|
19
|
+
# documented +call+ signatures. A run belongs to the thread that started it: a thread started
|
|
20
|
+
# from user code during a run doesn't see it.
|
|
21
|
+
class Run
|
|
22
|
+
# Fiber storage key holding the current run.
|
|
23
|
+
KEY = :price_waterfall_run
|
|
24
|
+
|
|
25
|
+
# @return [Time, nil] the moment given to the call, if any
|
|
26
|
+
attr_reader :now
|
|
27
|
+
|
|
28
|
+
# @return [Run, nil] the run in progress on this thread, if any
|
|
29
|
+
def self.current
|
|
30
|
+
run = Fiber[KEY]
|
|
31
|
+
run if run&.thread.equal?(Thread.current)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [Boolean] whether the run in progress, if any, writes condition reasons without values
|
|
35
|
+
def self.hiding_values? = current&.hide_values? || false
|
|
36
|
+
|
|
37
|
+
# Runs the block as a run, restoring the previous one afterwards, so a pipeline called from
|
|
38
|
+
# inside another (in a source or a custom step) gets a run of its own.
|
|
39
|
+
#
|
|
40
|
+
# @param now [Time, nil] see {#initialize}
|
|
41
|
+
# @param hide_values [Boolean] see {#initialize}
|
|
42
|
+
# @yieldparam run [Run]
|
|
43
|
+
# @return [Object] what the block returns
|
|
44
|
+
# @raise [InvalidAmount] if +now+ is not a Time
|
|
45
|
+
def self.start(now: nil, hide_values: false)
|
|
46
|
+
run = new(now:, hide_values:)
|
|
47
|
+
previous = Fiber[KEY]
|
|
48
|
+
Fiber[KEY] = run
|
|
49
|
+
begin
|
|
50
|
+
yield run
|
|
51
|
+
ensure
|
|
52
|
+
Fiber[KEY] = previous
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param now [Time, nil] the moment to price at; +nil+ asks the clocks
|
|
57
|
+
# @param hide_values [Boolean] write condition reasons without the values read
|
|
58
|
+
# @raise [InvalidAmount] if +now+ is not a Time
|
|
59
|
+
def initialize(now: nil, hide_values: false)
|
|
60
|
+
@now = Run.time!(now) unless nil.equal?(now)
|
|
61
|
+
@hide_values = hide_values
|
|
62
|
+
@thread = Thread.current
|
|
63
|
+
@times = {}.compare_by_identity
|
|
64
|
+
@results = {}.compare_by_identity
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @api private
|
|
68
|
+
# @return [Thread] the thread that started the run
|
|
69
|
+
attr_reader :thread
|
|
70
|
+
|
|
71
|
+
# @return [Boolean] whether condition reasons leave out the values they read
|
|
72
|
+
def hide_values? = @hide_values
|
|
73
|
+
|
|
74
|
+
# @param clock [#call] a step's clock
|
|
75
|
+
# @yield calls +clock+, the first time it is needed in this run
|
|
76
|
+
# @return [Object] {#now} when given, otherwise what +clock+ gave the first time it was asked
|
|
77
|
+
def time(clock)
|
|
78
|
+
return now if now
|
|
79
|
+
|
|
80
|
+
@times.fetch(clock) { @times[clock] = yield }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Calls user code once per callable and inputs, until {#next_step}.
|
|
84
|
+
#
|
|
85
|
+
# @param callable [Object] the source or operator, the first part of the key
|
|
86
|
+
# @param inputs [Array<Object>] what it is called with, compared by identity
|
|
87
|
+
# @yield computes the result; not kept if it raises
|
|
88
|
+
# @return [Object] the result, computed now or earlier in the run
|
|
89
|
+
def remember(callable, *inputs)
|
|
90
|
+
calls = (@results[callable] ||= [])
|
|
91
|
+
found = calls.find { |seen, _| seen.size == inputs.size && seen.zip(inputs).all? { |a, b| a.equal?(b) } }
|
|
92
|
+
return found.last if found
|
|
93
|
+
|
|
94
|
+
yield.tap { |result| calls << [inputs, result] }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Forgets the results kept by {#remember}, as a new step starts.
|
|
98
|
+
# @return [void]
|
|
99
|
+
def next_step = @results.clear
|
|
100
|
+
|
|
101
|
+
# @api private
|
|
102
|
+
# "Now" moves every time window, so only a Time is accepted (a time that says it is one, such
|
|
103
|
+
# as +ActiveSupport::TimeWithZone+, too). Parse a String yourself (+Time.iso8601+) after
|
|
104
|
+
# checking where it came from, so request data can't move the clock.
|
|
105
|
+
# @raise [InvalidAmount] unless +value+ is a Time
|
|
106
|
+
def self.time!(value, label = "now")
|
|
107
|
+
return value if value.is_a?(Time)
|
|
108
|
+
|
|
109
|
+
raise InvalidAmount, "#{label} must be a Time, got #{value.class} #{Conditions::Reason.show(value)} " \
|
|
110
|
+
"(parse Strings yourself, e.g. Time.iso8601, so request data can't set the time)"
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
# Wraps an exception raised inside user-supplied code (a callable or block) so the failing
|
|
5
|
+
# step is named in the message and in {Error#step}. The original exception is available as
|
|
6
|
+
# +cause+; Ruby sets it automatically when a StepError is raised inside the +rescue+ that
|
|
7
|
+
# caught the original.
|
|
8
|
+
class StepError < Error; end
|
|
9
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
# The rule for step names, shared by {Steps::Step} and {Entry} so a step and the entries it makes
|
|
5
|
+
# always have the same name: a non-blank Symbol or String that converts to UTF-8 (binary text is
|
|
6
|
+
# read as UTF-8 bytes), with no control characters, since a line break would break
|
|
7
|
+
# {Result#explain}'s one-line-per-step layout. The name is checked after it is converted, so a
|
|
8
|
+
# control character can't get in as bytes.
|
|
9
|
+
module StepName
|
|
10
|
+
# Said of a name that is not a Symbol or String, or is blank.
|
|
11
|
+
BLANK = "must be a non-blank Symbol or String"
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# @param name [Object]
|
|
16
|
+
# @param error [Class] the error to raise
|
|
17
|
+
# @param label [String] what the name is, for the message (+"step name"+, +"entry step"+)
|
|
18
|
+
# @return [Symbol] +name+ as a UTF-8 Symbol
|
|
19
|
+
# @raise [Error] of class +error+ when +name+ breaks the rule
|
|
20
|
+
# @example
|
|
21
|
+
# StepName.check!("rabais_\xC3\xA9t\xC3\xA9".b, ConfigurationError, "step name") # => :rabais_été
|
|
22
|
+
# StepName.check!("a\nb", ConfigurationError, "step name")
|
|
23
|
+
# # ConfigurationError: step name must not contain control characters such as line breaks, got "a\nb"
|
|
24
|
+
def check!(name, error, label)
|
|
25
|
+
text = utf8(name)
|
|
26
|
+
problem = problem(name, text)
|
|
27
|
+
return text.to_sym unless problem
|
|
28
|
+
|
|
29
|
+
raise error, "#{label} #{problem}, got #{Conditions::Reason.show(name)}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @param name [Object]
|
|
33
|
+
# @param text [String, nil] +name+ in UTF-8, as {.utf8} gives it
|
|
34
|
+
# @return [String, nil] what is wrong with +name+, or +nil+ when it is fine
|
|
35
|
+
def problem(name, text = utf8(name))
|
|
36
|
+
return BLANK unless name.is_a?(Symbol) || name.is_a?(String)
|
|
37
|
+
return "must be text that converts to UTF-8" if text.nil?
|
|
38
|
+
return BLANK if text.match?(/\A[[:space:]]*\z/)
|
|
39
|
+
|
|
40
|
+
"must not contain control characters such as line breaks" if text.match?(/[[:cntrl:]]/)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @param name [Object]
|
|
44
|
+
# @return [String, nil] +name+ in UTF-8, or +nil+ when it is not a Symbol or String, or its
|
|
45
|
+
# bytes are invalid or can't be converted. Nothing is replaced: a name must stay the same.
|
|
46
|
+
def utf8(name)
|
|
47
|
+
return unless name.is_a?(Symbol) || name.is_a?(String)
|
|
48
|
+
|
|
49
|
+
text = name.to_s
|
|
50
|
+
text = text.b.force_encoding(Encoding::UTF_8) if text.encoding == Encoding::BINARY
|
|
51
|
+
text.encode(Encoding::UTF_8) if text.valid_encoding?
|
|
52
|
+
rescue EncodingError
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Fixed discount in cents. A +nil+ or zero amount skips the step.
|
|
6
|
+
#
|
|
7
|
+
# When the amount is larger than the price, the price is capped at 0 by default; with
|
|
8
|
+
# +cap: false+ the step raises {InvalidAmount} instead. With capping, a price that is already
|
|
9
|
+
# 0 skips the step.
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# Amount.new(:loyalty, ->(_item, ctx) { ctx[:loyalty_off_cents] })
|
|
13
|
+
# # 4000 with 500 off -> 3500, trace note "-500 (fixed)"
|
|
14
|
+
# # 300 with 500 off -> 0, trace note "-300 (fixed 500, capped at price)"
|
|
15
|
+
class Amount < Callable
|
|
16
|
+
# @return [Boolean] whether an amount larger than the price caps the price at 0
|
|
17
|
+
attr_reader :cap
|
|
18
|
+
|
|
19
|
+
# Also takes +condition:+ / +skip_if:+, see {Step#initialize}.
|
|
20
|
+
#
|
|
21
|
+
# @param name [Symbol, String]
|
|
22
|
+
# @param input [#call, Object] callable +(item, ctx)+ or a literal amount in cents
|
|
23
|
+
# @param cap [Boolean] true to cap the price at 0, false to raise when the amount is larger
|
|
24
|
+
# @raise [ConfigurationError] if +cap+ is not true or false, or +input+ is invalid
|
|
25
|
+
def initialize(name, input, cap: true, **)
|
|
26
|
+
super(name, input, **)
|
|
27
|
+
configure do
|
|
28
|
+
next if [true, false].include?(cap)
|
|
29
|
+
|
|
30
|
+
raise ConfigurationError, "cap: must be true or false, got #{Conditions::Reason.show(cap)}"
|
|
31
|
+
end
|
|
32
|
+
@cap = cap
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def apply(cents, item, ctx)
|
|
38
|
+
off = value(item, ctx)
|
|
39
|
+
return skip(cents, "no amount") if off.nil? || off.zero?
|
|
40
|
+
return [cents - off, "-#{off} (fixed)"] if off <= cents
|
|
41
|
+
raise InvalidAmount, "amount #{off} is more than the price #{cents} (cap: false)" unless cap
|
|
42
|
+
return skip(cents, "price is 0") if cents.zero?
|
|
43
|
+
|
|
44
|
+
[0, "-#{cents} (fixed #{off}, capped at price)"]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def check_value(value) = value.nil? ? nil : cents!(value, "amount")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Starting price. Must be the first step of a pipeline; the price passed in is ignored.
|
|
6
|
+
# It can't be conditional or time-limited, since a pipeline always needs a starting price.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# Base.new(:base, ->(item, _ctx) { item[:list_price_cents] })
|
|
10
|
+
class Base < Callable
|
|
11
|
+
# Settings for conditions and time windows, which a starting price can't have.
|
|
12
|
+
UNSUPPORTED = %i[condition skip_if starts_at ends_at clock operators].freeze
|
|
13
|
+
|
|
14
|
+
# @param name [Symbol, String]
|
|
15
|
+
# @param input [#call, Hash, Object] callable +(item, ctx)+, a value description or a literal
|
|
16
|
+
# price in cents
|
|
17
|
+
# @param sources [Hash{Symbol, String => #call}] value sources, see {Step#initialize}
|
|
18
|
+
# @param unsupported [Hash] any of {UNSUPPORTED}; only +nil+ is allowed
|
|
19
|
+
# @raise [ConfigurationError] if any of {UNSUPPORTED} is set, or +input+ is invalid
|
|
20
|
+
def initialize(name, input, sources: {}, **unsupported)
|
|
21
|
+
super(name, input, sources:, **unsupported.except(*UNSUPPORTED))
|
|
22
|
+
configure do
|
|
23
|
+
given = unsupported.slice(*UNSUPPORTED).compact
|
|
24
|
+
unless given.empty?
|
|
25
|
+
raise ConfigurationError, "base can't take #{given.keys.map { |key| "#{key}:" }.join(", ")}; it is never " \
|
|
26
|
+
"skipped, as a pipeline always needs a starting price"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def apply(_cents, item, ctx) = [value(item, ctx), "base"]
|
|
34
|
+
|
|
35
|
+
def check_value(value) = cents!(value, "base price")
|
|
36
|
+
|
|
37
|
+
def input!(cents) = cents
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Abstract base for steps driven by one value: a callable +(item, ctx)+ or a literal.
|
|
6
|
+
# A literal is checked and converted once, when the step is built, so a bad one fails at
|
|
7
|
+
# define time and a good one costs nothing per call.
|
|
8
|
+
class Callable < Step
|
|
9
|
+
abstract!
|
|
10
|
+
|
|
11
|
+
# @return [#call] resolver for the step's value; for a literal, a {Values::Literal} holding
|
|
12
|
+
# the already checked and converted value
|
|
13
|
+
attr_reader :resolver
|
|
14
|
+
|
|
15
|
+
# Also takes +condition:+ / +skip_if:+, see {Step#initialize}.
|
|
16
|
+
#
|
|
17
|
+
# @param name [Symbol, String]
|
|
18
|
+
# @param input [#call, Hash, Object] callable +(item, ctx)+, a value description such as
|
|
19
|
+
# +{ ctx: "tier_discount" }+ (see {Resolver.describe}), or a literal value
|
|
20
|
+
# @raise [ConfigurationError] if +name+ or a literal +input+ is invalid, or a callable +input+
|
|
21
|
+
# does not take +(item, ctx)+
|
|
22
|
+
def initialize(name, input, **)
|
|
23
|
+
super(name, **)
|
|
24
|
+
configure do
|
|
25
|
+
resolver = Resolver.build(input, sources:, at: input_label)
|
|
26
|
+
@literal = resolver.is_a?(Values::Literal)
|
|
27
|
+
@resolver = @literal ? Values::Literal.new(check_value(resolver.value)) : resolver
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
# Names the input in error messages about its description.
|
|
34
|
+
def input_label = "value"
|
|
35
|
+
|
|
36
|
+
def value(item, ctx) = @literal ? resolver.value : check_value(invoke(resolver, item, ctx))
|
|
37
|
+
|
|
38
|
+
# Checks and converts a value from {#resolver}; subclasses override it.
|
|
39
|
+
# @raise [InvalidAmount]
|
|
40
|
+
def check_value(value) = value
|
|
41
|
+
|
|
42
|
+
# @param change [Array(Integer, String)] a price and note from {Percentage.adjust}
|
|
43
|
+
# @return [Array(Integer, String, Boolean)] the change, skipped when the share rounded to 0
|
|
44
|
+
# cents and left the price as it was
|
|
45
|
+
def adjusted(cents, (after, note)) = [after, note, after == cents]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Checks that a price is whole, non-negative cents, with a hint when it looks like a decimal
|
|
6
|
+
# amount of money.
|
|
7
|
+
module Cents
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
# @param value [Object]
|
|
11
|
+
# @param label [String] what the value is, for the error message
|
|
12
|
+
# @return [Integer] +value+
|
|
13
|
+
# @raise [InvalidAmount] unless +value+ is a non-negative Integer
|
|
14
|
+
# @example
|
|
15
|
+
# Cents.check!(45.99, "base price")
|
|
16
|
+
# # InvalidAmount: base price must be a non-negative Integer of cents, got Float 45.99;
|
|
17
|
+
# # convert to Integer cents first (e.g. 45.99 -> 4599)
|
|
18
|
+
def check!(value, label)
|
|
19
|
+
return value if value.is_a?(Integer) && !value.negative?
|
|
20
|
+
|
|
21
|
+
shown = Conditions::Reason.show(value)
|
|
22
|
+
# Arrays, Hashes and other objects already show what they are; a Set is written like a list.
|
|
23
|
+
shown = "#{value.class} #{shown}" unless value.is_a?(Array) || shown.start_with?("#<", "{")
|
|
24
|
+
raise InvalidAmount, "#{label} must be a non-negative Integer of cents, got #{shown}#{hint(value)}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @api private
|
|
28
|
+
def hint(value)
|
|
29
|
+
decimal = value.is_a?(Float) || value.is_a?(String) || (defined?(BigDecimal) && value.is_a?(BigDecimal))
|
|
30
|
+
decimal ? "; convert to Integer cents first (e.g. 45.99 -> 4599)" : ""
|
|
31
|
+
end
|
|
32
|
+
private_class_method :hint
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Escape hatch for logic the built-in steps don't cover. The callable or block gets
|
|
6
|
+
# +(cents, item, ctx)+ and returns +[new_cents, note]+.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# Custom.new(:min_margin) { |cents, item, _ctx| [[cents, item[:cost_cents]].max, "floor at cost"] }
|
|
10
|
+
class Custom < Step
|
|
11
|
+
# @return [#call] callable taking +(cents, item, ctx)+
|
|
12
|
+
attr_reader :callable
|
|
13
|
+
|
|
14
|
+
# Takes the callable as an argument or a block, not both. Also takes +condition:+ /
|
|
15
|
+
# +skip_if:+, see {Step#initialize}.
|
|
16
|
+
#
|
|
17
|
+
# @param name [Symbol, String]
|
|
18
|
+
# @param callable [#call, nil] callable taking +(cents, item, ctx)+
|
|
19
|
+
# @yieldparam cents [Integer]
|
|
20
|
+
# @yieldparam item [Object]
|
|
21
|
+
# @yieldparam ctx [Object]
|
|
22
|
+
# @yieldreturn [Array(Integer, String)] new price and trace note
|
|
23
|
+
# @raise [ConfigurationError] if there is no callable or block, both are given, or the
|
|
24
|
+
# callable cannot take +(cents, item, ctx)+
|
|
25
|
+
def initialize(name, callable = nil, **, &block)
|
|
26
|
+
super(name, **)
|
|
27
|
+
configure do
|
|
28
|
+
raise ConfigurationError, "takes a callable or a block, not both" unless callable.nil? || block.nil?
|
|
29
|
+
|
|
30
|
+
callable ||= block
|
|
31
|
+
unless callable.respond_to?(:call)
|
|
32
|
+
raise ConfigurationError, "needs a block or callable, got #{Conditions::Reason.show(callable)}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
Resolver.check_arity!(callable, 3, "(cents, item, ctx)")
|
|
36
|
+
end
|
|
37
|
+
@callable = callable
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def apply(cents, item, ctx)
|
|
43
|
+
result = invoke(callable, cents, item, ctx)
|
|
44
|
+
return result if result.is_a?(Array) && result.size == 2 && result.last.is_a?(String)
|
|
45
|
+
|
|
46
|
+
raise InvalidAmount, "must return [Integer cents, String note], got #{Conditions::Reason.show(result)}"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Replaces the price when its value is not +nil+ (e.g. a customer price list).
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# Override.new(:price_list, ->(item, ctx) { ctx.dig(:price_list, item[:sku]) })
|
|
9
|
+
# # trace notes: "no override" / "override (was 4599)"
|
|
10
|
+
class Override < Callable
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def apply(cents, item, ctx)
|
|
14
|
+
price = value(item, ctx)
|
|
15
|
+
return [cents, "no override", true] if price.nil?
|
|
16
|
+
|
|
17
|
+
[price, "override (was #{cents})"]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def check_value(value) = value.nil? ? nil : cents!(value, "override price")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Percentage discount. The discount is rounded half up to whole cents before it is subtracted.
|
|
6
|
+
# A +nil+ or zero percentage skips the step.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# Percent.new(:customer_tier, ->(_item, ctx) { ctx[:tier_discount] })
|
|
10
|
+
# # 4599 at 10% -> 4139, trace note "-460 (10%)"
|
|
11
|
+
class Percent < PercentageChange
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def increase? = false
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Abstract base for {Percent} and {Surcharge}: changes the price by a percentage, rounded half
|
|
6
|
+
# up to whole cents. A +nil+ or zero percentage skips the step. Discounts are capped at 100%;
|
|
7
|
+
# increases are not.
|
|
8
|
+
class PercentageChange < Callable
|
|
9
|
+
abstract!
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def apply(cents, item, ctx)
|
|
14
|
+
pct = value(item, ctx)
|
|
15
|
+
return skip(cents, "no percentage") if pct.nil? || pct.zero?
|
|
16
|
+
|
|
17
|
+
adjusted(cents, Percentage.adjust(cents, pct, increase: increase?))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def check_value(value) = Percentage.parse(value, max: increase? ? nil : 100)
|
|
21
|
+
|
|
22
|
+
# @return [Boolean] true to add the percentage, false to subtract it
|
|
23
|
+
def increase? = raise(NotImplementedError, "#{self.class} must implement #increase?")
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# Rounds the price to a multiple of +to+ cents. Named +:rounding+ unless a name is given.
|
|
6
|
+
# A name that is also a rounding mode (+Round.new(:floor)+) needs an explicit +mode:+, so it
|
|
7
|
+
# can't be mistaken for one.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# Round.new(mode: :half_up, to: 5) # 4139 -> 4140, trace note "half_up to 5"
|
|
11
|
+
# Round.new(:round_before_tax, mode: :floor)
|
|
12
|
+
class Round < Step
|
|
13
|
+
# @return [Rounding::Rounder] the checked rounding configuration
|
|
14
|
+
attr_reader :rounder
|
|
15
|
+
|
|
16
|
+
# Also takes +condition:+ / +skip_if:+, see {Step#initialize}.
|
|
17
|
+
#
|
|
18
|
+
# @param name [Symbol, String] step name, so a pipeline can round more than once
|
|
19
|
+
# @param mode [Symbol, String, nil] one of {Rounding::MODES}; +nil+ means +:half_up+
|
|
20
|
+
# @param to [Integer] round to a multiple of this many cents
|
|
21
|
+
# @raise [ConfigurationError] if +name+, +mode+ or +to+ is invalid, or +name+ is a rounding
|
|
22
|
+
# mode and +mode:+ is not given
|
|
23
|
+
def initialize(name = :rounding, mode: nil, to: 1, **)
|
|
24
|
+
super(name, **)
|
|
25
|
+
configure do
|
|
26
|
+
if mode.nil? && Rounding.normalize_mode(self.name)
|
|
27
|
+
raise ConfigurationError, "#{self.name.inspect} is a rounding mode, not a name; " \
|
|
28
|
+
"use mode: #{self.name.inspect} (or pass mode: to keep it as the name)"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
@rounder = Rounding.rounder(mode || :half_up, to:)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @return [Symbol] one of {Rounding::MODES}
|
|
36
|
+
def mode = rounder.mode
|
|
37
|
+
|
|
38
|
+
# @return [Integer] round to a multiple of this many cents
|
|
39
|
+
def to = rounder.to
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def apply(cents, _item, _ctx) = [rounder.call(cents), "#{mode} to #{to}"]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PriceWaterfall
|
|
4
|
+
module Steps
|
|
5
|
+
# @api private
|
|
6
|
+
# Decides whether a {Step} is skipped and writes the note saying why: outside its time window,
|
|
7
|
+
# or by its +condition:+ / +skip_if:+. Expects +window+, +condition+, +skip_if+ and +invoke+.
|
|
8
|
+
module Skipping
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# @return [String, nil] why the step is skipped, or +nil+ when it runs
|
|
12
|
+
def skip_note(item, ctx)
|
|
13
|
+
window_note || condition_note(item, ctx)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# "Now" comes from the run (see {Run#time}), so every step in it sees the same moment.
|
|
17
|
+
def window_note
|
|
18
|
+
window&.skip_note(Run.current.time(window.clock) { invoke(window.clock) })
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def condition_note(item, ctx)
|
|
22
|
+
return skipped("condition", condition, item, ctx, held: false) if condition && !invoke(condition, item, ctx)
|
|
23
|
+
|
|
24
|
+
skipped("skip_if", skip_if, item, ctx, held: true) if skip_if && invoke(skip_if, item, ctx)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A data condition says why it skipped the step (+skipped (condition: item.qty 4 < 10)+). The
|
|
28
|
+
# reason is worked out only now, on the skip path. The note is just the label instead
|
|
29
|
+
# ({Step::SKIPPED}, +skipped (skip_if)+) when the condition is a callable that can't say why,
|
|
30
|
+
# gives a different result this time (a registered source that changed), or fails while the
|
|
31
|
+
# note is written: the step was already skipped, and writing its note must never stop pricing.
|
|
32
|
+
def skipped(label, condition, item, ctx, held:)
|
|
33
|
+
plain = label == "condition" ? Step::SKIPPED : Step::SKIPPED_IF
|
|
34
|
+
return plain unless condition.is_a?(Conditions::Compiled)
|
|
35
|
+
|
|
36
|
+
again, why = condition.check(item, ctx)
|
|
37
|
+
again == held ? -"skipped (#{label}: #{why})" : plain
|
|
38
|
+
rescue StandardError
|
|
39
|
+
plain
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|