reason_me_this 0.1.0 → 0.3.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 +323 -16
- data/lib/generators/reason_me_this/receipts_generator.rb +17 -0
- data/lib/generators/reason_me_this/templates/create_reason_me_this_receipts.rb.tt +16 -0
- data/lib/reason_me_this/blueprint/calculation.rb +110 -0
- data/lib/reason_me_this/blueprint/fields.rb +50 -0
- data/lib/reason_me_this/blueprint/ruby.rb +217 -0
- data/lib/reason_me_this/blueprint/rules.rb +22 -0
- data/lib/reason_me_this/blueprint/step.rb +25 -0
- data/lib/reason_me_this/blueprint/yaml.rb +40 -0
- data/lib/reason_me_this/blueprint.rb +87 -0
- data/lib/reason_me_this/cli/output.rb +15 -2
- data/lib/reason_me_this/cli.rb +111 -27
- data/lib/reason_me_this/configuration.rb +9 -1
- data/lib/reason_me_this/engines.rb +25 -0
- data/lib/reason_me_this/errors.rb +8 -0
- data/lib/reason_me_this/explanation.rb +34 -1
- data/lib/reason_me_this/model.rb +52 -4
- data/lib/reason_me_this/receipt.rb +9 -0
- data/lib/reason_me_this/recipe.rb +35 -0
- data/lib/reason_me_this/saving.rb +30 -0
- data/lib/reason_me_this/values.rb +18 -0
- data/lib/reason_me_this/version.rb +1 -1
- data/lib/reason_me_this/webhook.rb +29 -0
- data/lib/reason_me_this.rb +61 -14
- metadata +17 -2
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
require 'bigdecimal'
|
|
2
|
+
require 'date'
|
|
3
|
+
require 'time'
|
|
4
|
+
|
|
5
|
+
module ReasonMeThis
|
|
6
|
+
class Blueprint
|
|
7
|
+
# Writes a blueprint's document as the Ruby that builds it, so that a blueprint kept as YAML can be kept as Ruby.
|
|
8
|
+
# What a method of the blueprint cannot say is written with step or input, which take anything.
|
|
9
|
+
class Ruby
|
|
10
|
+
class Unwritable < Error
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
WORD = /\A[a-z_][a-z0-9_]*\z/i
|
|
14
|
+
NAME = -> (held) { held.is_a?(String) }
|
|
15
|
+
NAMES = -> (held) { held.is_a?(Hash) && held.values.all?(String) }
|
|
16
|
+
NAME_LIST = -> (held) { held.is_a?(Array) && held.all?(String) }
|
|
17
|
+
FORMULA = -> (held) { held.is_a?(String) || held.is_a?(Numeric) || [true, false].include?(held) }
|
|
18
|
+
CONDITION = -> (held) { held.is_a?(String) || [true, false].include?(held) }
|
|
19
|
+
FORMULAS = -> (held) { held.is_a?(Hash) && held.values.all?(&FORMULA) }
|
|
20
|
+
TEXT = -> (held) { held.is_a?(String) }
|
|
21
|
+
ANYTHING = -> (held) { !held.nil? }
|
|
22
|
+
SYMBOLS = [NAME, NAMES, NAME_LIST].freeze
|
|
23
|
+
|
|
24
|
+
LITERALS = {BigDecimal => -> (held) { "BigDecimal('#{held.to_s('F')}')" },
|
|
25
|
+
Time => -> (held) { "Time.iso8601('#{held.iso8601}')" },
|
|
26
|
+
Date => -> (held) { "Date.new(#{held.year}, #{held.month}, #{held.day})" }}.freeze
|
|
27
|
+
|
|
28
|
+
def self.fits?(shape, spec)
|
|
29
|
+
shape && spec.is_a?(Hash) && ([*shape[:first], *shape[:needs]] - spec.keys).empty? &&
|
|
30
|
+
spec.all? { |key, held| shape[:keys].key?(key) && shape[:keys][key].call(held) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
RULE = {needs: [:name, :when], keys: {name: NAME, when: CONDITION, value: FORMULA}}.freeze
|
|
34
|
+
RULES = {needs: [:pick, :rules],
|
|
35
|
+
keys: {pick: NAME, otherwise: FORMULA,
|
|
36
|
+
rules: -> (held) {
|
|
37
|
+
held.is_a?(Array) && held.any? && held.all? do |rule|
|
|
38
|
+
rule.is_a?(Hash) && fits?(RULE, rule.transform_keys(&:to_sym))
|
|
39
|
+
end
|
|
40
|
+
}}}.freeze
|
|
41
|
+
|
|
42
|
+
ROWS = {with: FORMULAS, position: NAME, carry: NAMES, start: FORMULAS, earlier: NAME}.freeze
|
|
43
|
+
|
|
44
|
+
# What each step's method takes: the keys it puts first without their names, the keys it always writes, and
|
|
45
|
+
# every key with what its value must be to be written that way.
|
|
46
|
+
STEPS = {
|
|
47
|
+
formula: {first: [:formula], keys: {formula: FORMULA}},
|
|
48
|
+
lookup: {needs: [:table, :where], keys: {table: NAME, where: NAMES, value: NAME, on: NAME, band: NAMES, default: FORMULA}},
|
|
49
|
+
decide: {needs: [:table, :given, :hit], keys: {table: NAME, given: NAMES, hit: NAME, value: NAME, default: FORMULA}},
|
|
50
|
+
run: {first: [:calculation], keys: {calculation: NAME, over: NAME, **ROWS, update: -> (held) { held == true }}},
|
|
51
|
+
repeat: {first: [:calculation],
|
|
52
|
+
keys: {calculation: NAME, times: -> (held) { held.is_a?(String) || held.is_a?(Integer) }, until: NAME, limit: Integer.method(:===), **ROWS}},
|
|
53
|
+
sum: {first: [:output], needs: [:of], keys: {output: NAME, of: NAME}},
|
|
54
|
+
group: {needs: [:of, :by], keys: {of: NAME, by: NAME_LIST, sum: NAME_LIST}},
|
|
55
|
+
compare: {first: [:steps], needs: [:pick], keys: {steps: NAME_LIST, pick: NAME}},
|
|
56
|
+
slice: {keys: {from: TEXT, to: TEXT, over: NAME, without: ANYTHING, at_hours: -> (held) { held.is_a?(String) || NAME_LIST.call(held.map(&:to_s)) },
|
|
57
|
+
at_times: TEXT, within: ANYTHING, after: ANYTHING}}
|
|
58
|
+
}.freeze
|
|
59
|
+
|
|
60
|
+
TYPES = {number: [:default, :optional, :one_of], string: [:default, :optional, :one_of], date: [:default, :optional],
|
|
61
|
+
time: [:default, :optional], boolean: [:default, :optional], list: [:default, :optional, :of]}.freeze
|
|
62
|
+
|
|
63
|
+
def self.write(document, name) = new(document).write(name)
|
|
64
|
+
|
|
65
|
+
def initialize(document)
|
|
66
|
+
@document = document
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def write(name)
|
|
70
|
+
unknown(@document, %w[imports tables calculations], 'a blueprint')
|
|
71
|
+
parts = [imports, *tables, *@document.fetch('calculations', {}).map { |named, calculation| calculation(named, calculation) }].compact
|
|
72
|
+
constant = name.to_s.upcase.gsub(/[^A-Z0-9]+/, '_').sub(/\A(?=\d|\z)/, 'BLUEPRINT_')
|
|
73
|
+
"# typed: strict\n\n#{constant} = ReasonMeThis::Blueprint.define do\n#{indent(parts.join("\n\n"))}\nend #: ReasonMeThis::Blueprint\n"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def imports
|
|
79
|
+
names = @document['imports'] || []
|
|
80
|
+
"import #{names.map { |name| literal(name) }.join(', ')}" if names.any?
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def tables
|
|
84
|
+
@document.fetch('tables', {}).map do |name, rows|
|
|
85
|
+
"table #{symbol(name)}, [\n#{indent(rows.map { |row| "#{literal(row)}," }.join("\n"))}\n]"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def calculation(name, calculation)
|
|
90
|
+
unknown(calculation, %w[inputs steps outputs events], "calculation #{name}")
|
|
91
|
+
inputs = calculation['inputs'] || {}
|
|
92
|
+
parts = [
|
|
93
|
+
("inputs do\n#{indent(fields(inputs))}\nend" if inputs.any?),
|
|
94
|
+
calculation.fetch('steps', []).map { |step| step(step) }.join("\n"),
|
|
95
|
+
[*outputs(calculation['outputs'] || []), *calculation.fetch('events', []).map { |event| event(event) }].join("\n")
|
|
96
|
+
]
|
|
97
|
+
"calculation #{symbol(name)} do\n#{indent(parts.compact.reject(&:empty?).join("\n\n"))}\nend"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def fields(fields) = fields.map { |name, field| field(name, field) }.join("\n")
|
|
101
|
+
|
|
102
|
+
def field(name, field)
|
|
103
|
+
type = field['type'].to_s.to_sym
|
|
104
|
+
keys = field.except('type').transform_keys(&:to_sym)
|
|
105
|
+
return "input #{arguments([symbol(name), symbol(type)], keys.transform_values { |held| literal(held) })}" unless typed?(type, keys)
|
|
106
|
+
|
|
107
|
+
rows = keys.delete(:of)
|
|
108
|
+
call = "#{type} #{arguments([symbol(name)], keys.to_h { |key, held| [key, key == :one_of ? words(held) : literal(held)] })}"
|
|
109
|
+
rows ? "#{call} do\n#{indent(fields(rows))}\nend" : call
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def typed?(type, keys)
|
|
113
|
+
TYPES.key?(type) && (keys.keys - TYPES[type]).empty? && keys[:optional] != false && (!keys.key?(:default) || !keys[:default].nil?) &&
|
|
114
|
+
(!keys.key?(:of) || rows?(keys[:of]))
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def rows?(fields) = fields.is_a?(Hash) && fields.any? && fields.values.all?(Hash)
|
|
118
|
+
|
|
119
|
+
def words(one_of) = NAMES.call(one_of) ? symbols(one_of) : literal(one_of)
|
|
120
|
+
|
|
121
|
+
def step(step)
|
|
122
|
+
name = step['name']
|
|
123
|
+
types = step.keys - %w[name when else]
|
|
124
|
+
raise Unwritable.new("step #{name} has #{types.join(', ')}; a step has one step type") unless types.size == 1
|
|
125
|
+
raise Unwritable.new("step #{name} has else but no when") if step.key?('else') && !step.key?('when')
|
|
126
|
+
|
|
127
|
+
method, taken, block = call(name, types.first.to_sym, step[types.first])
|
|
128
|
+
return ["#{method} #{taken}", block].compact.join(' ') unless step.key?('when')
|
|
129
|
+
|
|
130
|
+
"#{method}(#{taken})#{" #{block}" if block}.when(#{arguments([literal(step['when'])], step.slice('else').transform_values { |held| literal(held) })})"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def call(name, type, spec)
|
|
134
|
+
spec = type == :formula ? {formula: spec} : keyed(spec)
|
|
135
|
+
return rules(name, spec) if type == :rules && Ruby.fits?(RULES, spec)
|
|
136
|
+
return written_by_type(name, type, spec) if Ruby.fits?(STEPS[type], spec)
|
|
137
|
+
|
|
138
|
+
[:step, [symbol(name), symbol(type), literal(type == :formula ? spec[:formula] : spec)].join(', '), nil]
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def keyed(spec) = spec.is_a?(Hash) ? spec.transform_keys(&:to_sym) : spec
|
|
142
|
+
|
|
143
|
+
def written_by_type(name, type, spec)
|
|
144
|
+
shape = STEPS[type]
|
|
145
|
+
first = shape.fetch(:first, [])
|
|
146
|
+
[type, arguments([symbol(name), *first.map { |key| written(shape, key, spec[key]) }],
|
|
147
|
+
spec.except(*first).to_h { |key, held| [key, written(shape, key, held)] }), nil]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def written(shape, key, held) = SYMBOLS.include?(shape[:keys][key]) ? symbols(held) : literal(held)
|
|
151
|
+
|
|
152
|
+
def rules(name, spec)
|
|
153
|
+
keys = {pick: symbol(spec[:pick]), **spec.slice(:otherwise).transform_values { |held| literal(held) }}
|
|
154
|
+
rules = spec[:rules].map do |rule|
|
|
155
|
+
"rule #{arguments([symbol(rule['name']), literal(rule['when'])], rule.slice('value').transform_values { |held| literal(held) })}"
|
|
156
|
+
end
|
|
157
|
+
[:rules, arguments([symbol(name)], keys), "do\n#{indent(rules.join("\n"))}\nend"]
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def outputs(outputs)
|
|
161
|
+
outputs.chunk_while { |earlier, later| earlier.is_a?(String) && later.is_a?(String) }.flat_map do |run|
|
|
162
|
+
run.first.is_a?(String) ? ["outputs #{run.map { |name| symbol(name) }.join(', ')}"] : run.map { |output| output(output) }
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def output(output)
|
|
167
|
+
unknown(output, %w[name type optional], "output #{output.is_a?(Hash) ? output['name'] : output.inspect}")
|
|
168
|
+
"output #{arguments([symbol(output['name'])], {type: output['type'] && symbol(output['type']), optional: output['optional']}.compact)}"
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def event(event)
|
|
172
|
+
unknown(event, %w[name when data], "event #{event['name']}")
|
|
173
|
+
raise Unwritable.new("event #{event['name']} needs when, the condition that sets it off") unless event['when'].is_a?(String)
|
|
174
|
+
|
|
175
|
+
"event #{arguments([symbol(event['name']), literal(event['when'])], event.slice('data').transform_values { |names| symbols(names) })}"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def unknown(held, keys, named)
|
|
179
|
+
raise Unwritable.new("#{named} should be a mapping") unless held.is_a?(Hash)
|
|
180
|
+
|
|
181
|
+
extra = held.keys - keys
|
|
182
|
+
raise Unwritable.new("#{named} has #{extra.join(', ')}, which cannot be written in Ruby; it takes #{keys.join(', ')}") if extra.any?
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def arguments(first, keys) = [*first, *keys.map { |key, held| "#{key}: #{held}" }].join(', ')
|
|
186
|
+
|
|
187
|
+
def symbols(held)
|
|
188
|
+
case held
|
|
189
|
+
when Hash then "{#{held.map { |key, name| "#{key(key)} #{symbol(name)}" }.join(', ')}}"
|
|
190
|
+
when Array then "[#{held.map { |name| symbol(name) }.join(', ')}]"
|
|
191
|
+
else symbol(held)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def symbol(name) = WORD.match?(name.to_s) ? ":#{name}" : ":#{name.to_s.inspect}"
|
|
196
|
+
|
|
197
|
+
def key(name) = WORD.match?(name.to_s) ? "#{name}:" : "#{name.to_s.inspect}:"
|
|
198
|
+
|
|
199
|
+
def literal(held)
|
|
200
|
+
case held
|
|
201
|
+
when Hash then "{#{held.map { |key, inner| "#{key(key)} #{literal(inner)}" }.join(', ')}}"
|
|
202
|
+
when Array then "[#{held.map { |inner| literal(inner) }.join(', ')}]"
|
|
203
|
+
else scalar(held)
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def scalar(held)
|
|
208
|
+
return held.match?(/['\\\n]|[^[:print:]]/) ? held.inspect : "'#{held}'" if held.is_a?(String)
|
|
209
|
+
|
|
210
|
+
written = LITERALS.find { |kind, _written| held.is_a?(kind) }
|
|
211
|
+
written ? written.last.call(held) : held.inspect
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def indent(text) = text.gsub(/^(?=.)/, ' ')
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
|
|
3
|
+
module ReasonMeThis
|
|
4
|
+
class Blueprint
|
|
5
|
+
# The rules of a rules step, in the order they are tried.
|
|
6
|
+
class Rules
|
|
7
|
+
#: -> void
|
|
8
|
+
def initialize
|
|
9
|
+
@rules = [] #: Array[Hash[Symbol, untyped]]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# A rule without a value gives its name.
|
|
13
|
+
#: (Symbol, String | bool, ?value: (String | Numeric | bool)?) -> void
|
|
14
|
+
def rule(name, condition, value: nil)
|
|
15
|
+
@rules << {name:, when: condition, value:}.compact
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
#: -> Array[Hash[Symbol, untyped]]
|
|
19
|
+
def to_a = @rules
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
|
|
3
|
+
module ReasonMeThis
|
|
4
|
+
class Blueprint
|
|
5
|
+
# A step of a calculation, as its method hands it back, so that it can be given a condition.
|
|
6
|
+
class Step
|
|
7
|
+
#: (Symbol, Symbol, untyped) -> void
|
|
8
|
+
def initialize(name, type, spec)
|
|
9
|
+
@step = {name:, type => spec} #: Hash[Symbol, untyped]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# The step runs only when the condition is true. Skipped, its value is the else formula, or blank without one.
|
|
13
|
+
#: (String, ?else: (String | Numeric | bool)?) -> Step
|
|
14
|
+
def when(condition, else: nil)
|
|
15
|
+
@step[:when] = condition
|
|
16
|
+
otherwise = binding.local_variable_get(:else)
|
|
17
|
+
@step[:else] = otherwise unless otherwise.nil?
|
|
18
|
+
self
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
#: -> Hash[Symbol, untyped]
|
|
22
|
+
def to_h = @step
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# typed: false
|
|
2
|
+
|
|
3
|
+
require 'bigdecimal'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
|
|
6
|
+
module ReasonMeThis
|
|
7
|
+
class Blueprint
|
|
8
|
+
# Reads and writes a blueprint's document as the YAML a blueprint file holds. Written, a decimal as the exact number it is, and a
|
|
9
|
+
# value used twice in full both times, since a blueprint may not have an alias.
|
|
10
|
+
class Yaml < Psych::Visitors::YAMLTree
|
|
11
|
+
class Exact < Psych::ScalarScanner
|
|
12
|
+
def tokenize(text)
|
|
13
|
+
read = super
|
|
14
|
+
read.is_a?(Float) && text.match?(/\A[-+]?\d[\d_]*\.\d+\z/) ? BigDecimal(text.delete('_')) : read
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Reads a blueprint file's document as the engine does: decimals exactly, dates as dates, and every key as text,
|
|
19
|
+
# so that on, yes and no are names and never true or false.
|
|
20
|
+
def self.read(yaml)
|
|
21
|
+
tree = Psych.parse(yaml) or return
|
|
22
|
+
tree.grep(Psych::Nodes::Mapping).each { |mapping| mapping.children.each_slice(2) { |key, _held| key.quoted = true if key.scalar? } }
|
|
23
|
+
loader = Psych::ClassLoader::Restricted.new(%w[Date Time], [])
|
|
24
|
+
Psych::Visitors::ToRuby.new(Exact.new(loader), loader).accept(tree)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.write(document)
|
|
28
|
+
tree = create
|
|
29
|
+
tree << document
|
|
30
|
+
tree.tree.yaml.delete_prefix("---\n")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def register(_target, node) = node
|
|
34
|
+
|
|
35
|
+
def visit_BigDecimal(decimal) # rubocop:disable Naming/MethodName
|
|
36
|
+
@emitter.scalar(decimal.to_s('F'), nil, nil, true, false, Psych::Nodes::Scalar::PLAIN)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
require 'reason_me_this/blueprint/fields'
|
|
5
|
+
require 'reason_me_this/blueprint/rules'
|
|
6
|
+
require 'reason_me_this/blueprint/step'
|
|
7
|
+
require 'reason_me_this/blueprint/calculation'
|
|
8
|
+
|
|
9
|
+
module ReasonMeThis
|
|
10
|
+
# A blueprint written in Ruby. It builds the document a blueprint file holds, so it is pushed, checked and run
|
|
11
|
+
# as one is.
|
|
12
|
+
class Blueprint
|
|
13
|
+
#: { () [self: Blueprint] -> void } -> Blueprint
|
|
14
|
+
def self.define(&block)
|
|
15
|
+
blueprint = new
|
|
16
|
+
blueprint.instance_eval(&block)
|
|
17
|
+
blueprint
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The document with text for every name, and a time as the ISO 8601 text a blueprint file holds one as.
|
|
21
|
+
#: (untyped) -> untyped
|
|
22
|
+
def self.plain(value)
|
|
23
|
+
case value
|
|
24
|
+
when Hash then value.to_h { |key, held| [key.to_s, plain(held)] }
|
|
25
|
+
when Array then value.map { |held| plain(held) }
|
|
26
|
+
when Symbol then value.to_s
|
|
27
|
+
when Time then value.iso8601
|
|
28
|
+
else value
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
#: -> void
|
|
33
|
+
def initialize
|
|
34
|
+
@imports = [] #: Array[String]
|
|
35
|
+
@tables = {} #: Hash[Symbol, Array[Hash[Symbol, untyped]]]
|
|
36
|
+
@calculations = {} #: Hash[Symbol, Calculation]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A path to a blueprint file, or the name of a blueprint the service or the engine knows.
|
|
40
|
+
#: (*String) -> void
|
|
41
|
+
def import(*names)
|
|
42
|
+
@imports.concat(names)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
#: (Symbol, Array[Hash[Symbol, untyped]]) -> void
|
|
46
|
+
def table(name, rows)
|
|
47
|
+
@tables[name] = rows
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The first calculation is the one run when none is named.
|
|
51
|
+
#: (Symbol) { () [self: Calculation] -> void } -> void
|
|
52
|
+
def calculation(name, &block)
|
|
53
|
+
calculation = Calculation.new
|
|
54
|
+
calculation.instance_eval(&block)
|
|
55
|
+
@calculations[name] = calculation
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
#: -> Hash[String, untyped]
|
|
59
|
+
def to_h
|
|
60
|
+
document = {imports: @imports, tables: @tables}.reject { |_key, held| held.empty? }
|
|
61
|
+
Blueprint.plain({**document, calculations: @calculations.transform_values(&:to_h)})
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
#: -> String
|
|
65
|
+
def yaml = written(to_h)
|
|
66
|
+
|
|
67
|
+
# The examples its calculations give, as the cases a cases file holds.
|
|
68
|
+
#: -> Array[Hash[String, untyped]]
|
|
69
|
+
def cases
|
|
70
|
+
default = @calculations.keys.first
|
|
71
|
+
Blueprint.plain(@calculations.flat_map do |name, calculation|
|
|
72
|
+
calculation.examples.map { |example| name == default ? example : {**example, calculation: name} }
|
|
73
|
+
end)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
#: -> String
|
|
77
|
+
def cases_yaml = written(cases)
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
#: (untyped) -> String
|
|
82
|
+
def written(document)
|
|
83
|
+
require 'reason_me_this/blueprint/yaml'
|
|
84
|
+
Yaml.write(document)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -41,9 +41,18 @@ module ReasonMeThis
|
|
|
41
41
|
def outputs(values) = fields(values, 0).join("\n")
|
|
42
42
|
|
|
43
43
|
def trace(explained)
|
|
44
|
-
[outputs(explained[:outputs]), '', bold('How it was worked out'), *steps(explained[:steps], 1)].join("\n")
|
|
44
|
+
[outputs(explained[:outputs]), '', bold('How it was worked out'), *steps(explained[:steps], 1), *events(Array(explained[:events]))].join("\n")
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
+
def events(list)
|
|
48
|
+
return [] if list.empty?
|
|
49
|
+
|
|
50
|
+
width = list.map { |event| event[:name].to_s.size }.max
|
|
51
|
+
['', bold('Sets off'), *list.map { |event| " #{event[:name].to_s.ljust(width)} #{carried(event[:data])}".rstrip }]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def carried(data) = data.map { |name, value| "#{name} #{value}" }.join(', ')
|
|
55
|
+
|
|
47
56
|
def problems(answer)
|
|
48
57
|
listed = answer[:problems]
|
|
49
58
|
return pass('No problems') if listed.empty?
|
|
@@ -76,7 +85,11 @@ module ReasonMeThis
|
|
|
76
85
|
['', " #{fail(result[:name])}", *detail]
|
|
77
86
|
end
|
|
78
87
|
|
|
79
|
-
def published(answer, name:)
|
|
88
|
+
def published(answer, name:)
|
|
89
|
+
waiting = answer.key?(:live_version) && answer[:live_version] != answer[:version]
|
|
90
|
+
live = answer[:live_version] ? "v#{answer[:live_version]} is live" : 'none is live'
|
|
91
|
+
pass("Published #{name} v#{answer[:version]}#{", in effect from #{answer[:effective_from]}; #{live}" if waiting}")
|
|
92
|
+
end
|
|
80
93
|
|
|
81
94
|
def definitions(answer)
|
|
82
95
|
rows = answer[:definitions].map do |definition|
|