qube_sync 0.4.0 → 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/CHANGELOG.md +38 -2
- data/README.md +58 -2
- data/exe/qube_sync +11 -0
- data/lib/qube_sync/client.rb +40 -5
- data/lib/qube_sync/operations/account.rb +21 -20
- data/lib/qube_sync/operations/ar_refund_credit_card.rb +17 -14
- data/lib/qube_sync/operations/bill.rb +148 -104
- data/lib/qube_sync/operations/bill_payment_check.rb +29 -22
- data/lib/qube_sync/operations/bill_payment_credit_card.rb +21 -16
- data/lib/qube_sync/operations/bill_to_pay.rb +7 -6
- data/lib/qube_sync/operations/billing_rate.rb +10 -9
- data/lib/qube_sync/operations/build_assembly.rb +13 -12
- data/lib/qube_sync/operations/charge.rb +36 -34
- data/lib/qube_sync/operations/check.rb +144 -100
- data/lib/qube_sync/operations/class.rb +11 -11
- data/lib/qube_sync/operations/credit_card_charge.rb +144 -100
- data/lib/qube_sync/operations/credit_card_credit.rb +144 -100
- data/lib/qube_sync/operations/credit_memo.rb +142 -108
- data/lib/qube_sync/operations/currency.rb +7 -7
- data/lib/qube_sync/operations/customer.rb +54 -53
- data/lib/qube_sync/operations/customer_msg.rb +5 -5
- data/lib/qube_sync/operations/customer_type.rb +7 -7
- data/lib/qube_sync/operations/data_ext.rb +7 -6
- data/lib/qube_sync/operations/date_driven_terms.rb +5 -5
- data/lib/qube_sync/operations/deposit.rb +53 -38
- data/lib/qube_sync/operations/employee.rb +27 -23
- data/lib/qube_sync/operations/entity.rb +5 -5
- data/lib/qube_sync/operations/estimate.rb +119 -90
- data/lib/qube_sync/operations/fields.rb +4058 -0
- data/lib/qube_sync/operations/form1099_category_account_mapping.rb +3 -2
- data/lib/qube_sync/operations/inventory_adjustment.rb +43 -34
- data/lib/qube_sync/operations/inventory_site.rb +11 -11
- data/lib/qube_sync/operations/invoice.rb +142 -108
- data/lib/qube_sync/operations/item.rb +5 -5
- data/lib/qube_sync/operations/item_assemblies_can_build.rb +2 -2
- data/lib/qube_sync/operations/item_discount.rb +25 -24
- data/lib/qube_sync/operations/item_fixed_asset.rb +17 -16
- data/lib/qube_sync/operations/item_group.rb +17 -15
- data/lib/qube_sync/operations/item_inventory.rb +45 -44
- data/lib/qube_sync/operations/item_inventory_assembly.rb +51 -48
- data/lib/qube_sync/operations/item_non_inventory.rb +55 -44
- data/lib/qube_sync/operations/item_other_charge.rb +51 -40
- data/lib/qube_sync/operations/item_payment.rb +21 -20
- data/lib/qube_sync/operations/item_receipt.rb +144 -100
- data/lib/qube_sync/operations/item_sales_tax.rb +21 -20
- data/lib/qube_sync/operations/item_sales_tax_group.rb +11 -11
- data/lib/qube_sync/operations/item_service.rb +55 -44
- data/lib/qube_sync/operations/item_sites.rb +10 -8
- data/lib/qube_sync/operations/item_subtotal.rb +7 -7
- data/lib/qube_sync/operations/job_type.rb +7 -7
- data/lib/qube_sync/operations/journal_entry.rb +49 -34
- data/lib/qube_sync/operations/lead.rb +7 -7
- data/lib/qube_sync/operations/list.rb +2 -2
- data/lib/qube_sync/operations/list_display.rb +2 -2
- data/lib/qube_sync/operations/list_merge.rb +6 -4
- data/lib/qube_sync/operations/other_name.rb +7 -7
- data/lib/qube_sync/operations/payment_method.rb +6 -5
- data/lib/qube_sync/operations/payroll_item_non_wage.rb +5 -5
- data/lib/qube_sync/operations/payroll_item_wage.rb +7 -7
- data/lib/qube_sync/operations/price_level.rb +23 -19
- data/lib/qube_sync/operations/purchase_order.rb +113 -86
- data/lib/qube_sync/operations/receive_payment.rb +37 -30
- data/lib/qube_sync/operations/sales_order.rb +117 -90
- data/lib/qube_sync/operations/sales_receipt.rb +142 -108
- data/lib/qube_sync/operations/sales_rep.rb +11 -11
- data/lib/qube_sync/operations/sales_tax_code.rb +15 -15
- data/lib/qube_sync/operations/sales_tax_payment_check.rb +18 -14
- data/lib/qube_sync/operations/sales_tax_return_line.rb +2 -2
- data/lib/qube_sync/operations/ship_method.rb +5 -5
- data/lib/qube_sync/operations/special_account.rb +2 -2
- data/lib/qube_sync/operations/standard_terms.rb +5 -5
- data/lib/qube_sync/operations/terms.rb +5 -5
- data/lib/qube_sync/operations/time_tracking.rb +23 -22
- data/lib/qube_sync/operations/to_do.rb +23 -23
- data/lib/qube_sync/operations/transaction.rb +15 -10
- data/lib/qube_sync/operations/transfer.rb +12 -12
- data/lib/qube_sync/operations/transfer_inventory.rb +35 -26
- data/lib/qube_sync/operations/txn_display.rb +2 -2
- data/lib/qube_sync/operations/unit_of_measure_set.rb +5 -5
- data/lib/qube_sync/operations/vehicle.rb +7 -7
- data/lib/qube_sync/operations/vehicle_mileage.rb +8 -8
- data/lib/qube_sync/operations/vendor.rb +54 -52
- data/lib/qube_sync/operations/vendor_credit.rb +144 -100
- data/lib/qube_sync/operations/vendor_type.rb +7 -7
- data/lib/qube_sync/operations/workers_comp_code.rb +8 -8
- data/lib/qube_sync/operations.rb +4 -0
- data/lib/qube_sync/version.rb +1 -1
- data/lib/qube_sync/workflow_types/cli.rb +120 -0
- data/lib/qube_sync/workflow_types/rbs_renderer.rb +290 -0
- data/lib/qube_sync/workflow_types/ruby_renderer.rb +391 -0
- data/lib/qube_sync/workflow_types/workflow.rb +153 -0
- data/lib/qube_sync/workflow_types.rb +219 -0
- data/sig/qube_sync/client.rbs +4 -1
- data/sig/qube_sync/operations.rbs +1 -0
- data/sig/qube_sync/workflow_types.rbs +59 -0
- metadata +12 -3
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module QubeSync
|
|
4
|
+
module WorkflowTypes
|
|
5
|
+
# Writes the Ruby half of a render: QubeWorkflows, a module per workflow, its start and input
|
|
6
|
+
# methods, documented with YARD. Plain Ruby that loads on 2.7 as well, since qube_rails writes
|
|
7
|
+
# the same file into Rails apps: no endless methods, no `it`, no hash shorthand.
|
|
8
|
+
class RubyRenderer
|
|
9
|
+
RUBY_KEYWORDS = %w[
|
|
10
|
+
alias and begin break case class def do else elsif end ensure false for if in module next nil not
|
|
11
|
+
or redo rescue retry return self super then true undef unless until when while yield
|
|
12
|
+
].freeze
|
|
13
|
+
# A start method's own parameters: an input named after one takes a trailing underscore in
|
|
14
|
+
# its methods (`version_:`), and keeps its name in the input.
|
|
15
|
+
RUN_PARAMS = %w[connection reference webhook_url version client].freeze
|
|
16
|
+
|
|
17
|
+
# input: the Workflow::Input; name: its parameter's name; positional: taken positionally.
|
|
18
|
+
Param = Struct.new(:input, :name, :positional, keyword_init: true)
|
|
19
|
+
|
|
20
|
+
# The body first (positional), then the other inputs as keywords, sorted. A name that's a
|
|
21
|
+
# start's own, or a Ruby keyword taken positionally, takes a trailing underscore. The RBS
|
|
22
|
+
# names its parameters the same.
|
|
23
|
+
def self.params_for(inputs, body)
|
|
24
|
+
taken = inputs.map(&:name)
|
|
25
|
+
ordered = body ? [body] + (inputs - [body]) : inputs
|
|
26
|
+
ordered.map do |input|
|
|
27
|
+
positional = input.equal?(body)
|
|
28
|
+
name = input.name
|
|
29
|
+
if RUN_PARAMS.include?(name) || (positional && RUBY_KEYWORDS.include?(name))
|
|
30
|
+
name = "#{name}_"
|
|
31
|
+
name = "#{name}_" while taken.include?(name)
|
|
32
|
+
taken << name
|
|
33
|
+
end
|
|
34
|
+
Param.new(input: input, name: name, positional: positional)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def initialize(workflows, header:, client:)
|
|
39
|
+
@workflows = workflows
|
|
40
|
+
@header = header
|
|
41
|
+
@client = client
|
|
42
|
+
@setter = client == DEFAULT_CLIENT
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def render
|
|
46
|
+
lines = @header + ["# frozen_string_literal: true", ""]
|
|
47
|
+
lines.concat(top_doc)
|
|
48
|
+
lines << "module QubeWorkflows"
|
|
49
|
+
lines.concat(client_accessors) if @setter
|
|
50
|
+
lines.concat(connection_id_method)
|
|
51
|
+
@workflows.each do |workflow|
|
|
52
|
+
lines << ""
|
|
53
|
+
lines.concat(accessor(workflow))
|
|
54
|
+
end
|
|
55
|
+
@workflows.each do |workflow|
|
|
56
|
+
lines << ""
|
|
57
|
+
lines.concat(workflow_module(workflow))
|
|
58
|
+
end
|
|
59
|
+
lines.concat(["end", ""])
|
|
60
|
+
lines.join("\n")
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def top_doc
|
|
66
|
+
client = @client.delete_prefix("::")
|
|
67
|
+
text = [
|
|
68
|
+
"Your app's QuBe Sync workflows, from their input types: QubeWorkflows.<workflow> is a workflow's module, " \
|
|
69
|
+
"with a start method per branch of its input (QubeWorkflows.<workflow>.<branch>(connection, body, ..., " \
|
|
70
|
+
"reference:, webhook_url:)), or start(connection, <inputs>, reference:, webhook_url:) for a workflow " \
|
|
71
|
+
"whose input has no branches. Each <branch>_input (or input) returns the input alone, as a Hash " \
|
|
72
|
+
"without its nil values. connection is a connection's QuBe Sync id, or anything with a qube_id " \
|
|
73
|
+
"(a QubeRails::Connection).",
|
|
74
|
+
if @setter
|
|
75
|
+
"A start uses QubeWorkflows.client unless it's passed another as client:. Set it once, at boot: " \
|
|
76
|
+
"QubeWorkflows.client = QubeSync::Client.new(api_key: ENV.fetch(\"QUBE_API_KEY\"))."
|
|
77
|
+
else
|
|
78
|
+
"A start uses #{client} unless it's passed another as client:."
|
|
79
|
+
end,
|
|
80
|
+
"The signatures are in sig/qube_workflows.rbs. An operation's body is typed as that operation's " \
|
|
81
|
+
"params, except that a field the workflow sets itself is optional (and a value given for it is " \
|
|
82
|
+
"overwritten): QuBe Sync checks an input's fields and types when the run starts, and required " \
|
|
83
|
+
"fields when it sends the request."
|
|
84
|
+
]
|
|
85
|
+
text.flat_map { |paragraph| ["#"] + WorkflowTypes.wrap(paragraph, "# ", "# ") }.drop(1)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def client_accessors
|
|
89
|
+
[
|
|
90
|
+
" class << self",
|
|
91
|
+
" attr_writer :client",
|
|
92
|
+
"",
|
|
93
|
+
" # The QubeSync::Client a start uses unless given client:.",
|
|
94
|
+
" def client",
|
|
95
|
+
" @client || raise(::ArgumentError, \"QubeWorkflows.client isn't set: set it once, at boot \" \\",
|
|
96
|
+
" \"(QubeWorkflows.client = QubeSync::Client.new(api_key: ...)), or pass client:\")",
|
|
97
|
+
" end",
|
|
98
|
+
" end",
|
|
99
|
+
""
|
|
100
|
+
]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def connection_id_method
|
|
104
|
+
[
|
|
105
|
+
" # A connection's QuBe Sync id: connection itself, or its qube_id when it has one (a",
|
|
106
|
+
" # QubeRails::Connection).",
|
|
107
|
+
" def self.connection_id(connection)",
|
|
108
|
+
" connection.respond_to?(:qube_id) ? connection.qube_id : connection",
|
|
109
|
+
" end"
|
|
110
|
+
]
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def accessor(workflow)
|
|
114
|
+
[
|
|
115
|
+
" # #{title(workflow)} (`#{workflow.key}`): QubeWorkflows::#{workflow.const}.",
|
|
116
|
+
" def self.#{workflow.accessor}",
|
|
117
|
+
" #{workflow.const}",
|
|
118
|
+
" end"
|
|
119
|
+
]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def title(workflow)
|
|
123
|
+
workflow.name.strip.empty? ? workflow.key : workflow.name.split.join(" ")
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def workflow_module(workflow)
|
|
127
|
+
version = workflow.draft ? "a draft, not published yet" : "version #{workflow.version.inspect}"
|
|
128
|
+
intro = "#{title(workflow)} (`#{workflow.key}`, #{version})."
|
|
129
|
+
intro = "#{intro} #{workflow.description}" unless workflow.description.strip.empty?
|
|
130
|
+
lines = WorkflowTypes.wrap(intro, " # ", " # ")
|
|
131
|
+
if workflow.discriminated?
|
|
132
|
+
names = workflow.branches.map { |branch| "#{branch.method} (#{branch.value.inspect})" }
|
|
133
|
+
lines << " #"
|
|
134
|
+
lines.concat(WorkflowTypes.wrap("A start method per `#{workflow.discriminator}`: #{names.join(", ")}. " \
|
|
135
|
+
"start takes any of their inputs.", " # ", " # "))
|
|
136
|
+
end
|
|
137
|
+
lines << " module #{workflow.const}"
|
|
138
|
+
lines << " WORKFLOW = #{workflow.key.inspect}"
|
|
139
|
+
lines << " # The version these types describe (nil for a draft). A start runs the published version"
|
|
140
|
+
lines << " # unless given version:."
|
|
141
|
+
lines << " VERSION = #{workflow.version.inspect}"
|
|
142
|
+
if workflow.discriminated?
|
|
143
|
+
workflow.branches.each do |branch|
|
|
144
|
+
lines << ""
|
|
145
|
+
lines.concat(branch_method(workflow, branch))
|
|
146
|
+
lines << ""
|
|
147
|
+
lines.concat(branch_input_method(workflow, branch))
|
|
148
|
+
end
|
|
149
|
+
lines << ""
|
|
150
|
+
lines.concat(start_any_method(workflow))
|
|
151
|
+
else
|
|
152
|
+
lines << ""
|
|
153
|
+
lines.concat(start_method(workflow))
|
|
154
|
+
lines << ""
|
|
155
|
+
lines.concat(input_method(workflow))
|
|
156
|
+
end
|
|
157
|
+
lines << " end"
|
|
158
|
+
lines
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# -- Methods ----------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
def branch_method(workflow, branch)
|
|
164
|
+
params = params_for(branch.inputs, branch.body)
|
|
165
|
+
doc = ["Starts #{workflow.key} with `#{workflow.discriminator}` #{branch.value.inspect}."]
|
|
166
|
+
lines = doc_lines(doc, [connection_tag] + input_tags(params) + run_tags + return_tags + see_tags(branch.inputs))
|
|
167
|
+
call = call_args(params)
|
|
168
|
+
lines << " def self.#{branch.method}(#{(["connection"] + signature(params) + run_signature).join(", ")})"
|
|
169
|
+
lines << " start(connection, #{branch.method}_input(#{call}), reference: reference, webhook_url: webhook_url, " \
|
|
170
|
+
"version: version, client: client)"
|
|
171
|
+
lines << " end"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def branch_input_method(workflow, branch)
|
|
175
|
+
params = params_for(branch.inputs, branch.body)
|
|
176
|
+
doc = ["#{workflow.key}'s input with `#{workflow.discriminator}` #{branch.value.inspect}, without its nil " \
|
|
177
|
+
"values: for start, or a QubeRails::Operation's input."]
|
|
178
|
+
lines = doc_lines(doc, input_tags(params) + ["@return [Hash] the input"] + see_tags(branch.inputs))
|
|
179
|
+
pairs = ["#{label(workflow.discriminator)} #{branch.value.inspect}"] + pairs(params)
|
|
180
|
+
lines << " def self.#{branch.method}_input(#{signature(params).join(", ")})"
|
|
181
|
+
lines << " {#{pairs.join(", ")}}.compact"
|
|
182
|
+
lines << " end"
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def start_any_method(workflow)
|
|
186
|
+
inputs = workflow.branches.map { |branch| "#{branch.method}_input" }
|
|
187
|
+
doc = ["Starts #{workflow.key} with any of its inputs: a Hash as #{inputs.join(" or ")} returns it."]
|
|
188
|
+
tags = [connection_tag, "@param input [Hash] the input"] + run_tags + return_tags
|
|
189
|
+
lines = doc_lines(doc, tags)
|
|
190
|
+
lines << " def self.start(#{(["connection", "input"] + run_signature).join(", ")})"
|
|
191
|
+
lines << " client.start_run(::QubeWorkflows.connection_id(connection), WORKFLOW, input, reference: reference, " \
|
|
192
|
+
"webhook_url: webhook_url, version: version)"
|
|
193
|
+
lines << " end"
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def start_method(workflow)
|
|
197
|
+
params = params_for(workflow.inputs, nil)
|
|
198
|
+
doc = ["Starts #{workflow.key}."]
|
|
199
|
+
lines = doc_lines(doc, [connection_tag] + input_tags(params) + run_tags + return_tags + see_tags(workflow.inputs))
|
|
200
|
+
lines << " def self.start(#{(["connection"] + signature(params) + run_signature).join(", ")})"
|
|
201
|
+
lines << " client.start_run(::QubeWorkflows.connection_id(connection), WORKFLOW, input(#{call_args(params)}), " \
|
|
202
|
+
"reference: reference, webhook_url: webhook_url, version: version)"
|
|
203
|
+
lines << " end"
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def input_method(workflow)
|
|
207
|
+
params = params_for(workflow.inputs, nil)
|
|
208
|
+
doc = ["#{workflow.key}'s input, without its nil values: for start, or a QubeRails::Operation's input."]
|
|
209
|
+
lines = doc_lines(doc, input_tags(params) + ["@return [Hash] the input"] + see_tags(workflow.inputs))
|
|
210
|
+
lines << " def self.input(#{signature(params).join(", ")})"
|
|
211
|
+
lines << " {#{pairs(params).join(", ")}}.compact"
|
|
212
|
+
lines << " end"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# -- Parameters -------------------------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
def params_for(inputs, body)
|
|
218
|
+
self.class.params_for(inputs, body)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def signature(params)
|
|
222
|
+
params.map do |param|
|
|
223
|
+
if param.positional
|
|
224
|
+
param.name
|
|
225
|
+
elsif param.input.required
|
|
226
|
+
"#{param.name}:"
|
|
227
|
+
else
|
|
228
|
+
"#{param.name}: nil"
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def run_signature
|
|
234
|
+
["reference:", "webhook_url:", "version: nil", "client: #{@client}"]
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# A parameter's value: a keyword that's also a Ruby keyword (`class:`) is read through the
|
|
238
|
+
# binding.
|
|
239
|
+
def value(param)
|
|
240
|
+
(!param.positional && RUBY_KEYWORDS.include?(param.name)) ? "binding.local_variable_get(:#{param.name})" : param.name
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def call_args(params)
|
|
244
|
+
params.map { |param| param.positional ? value(param) : "#{param.name}: #{value(param)}" }.join(", ")
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def pairs(params)
|
|
248
|
+
params.map { |param| "#{label(param.input.name)} #{value(param)}" }
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def label(name)
|
|
252
|
+
"#{name}:"
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# -- YARD -------------------------------------------------------------------------------
|
|
256
|
+
|
|
257
|
+
def doc_lines(paragraphs, tags)
|
|
258
|
+
lines = paragraphs.flat_map { |text| WorkflowTypes.wrap(text, " # ", " # ") }
|
|
259
|
+
lines << " #"
|
|
260
|
+
tags.each { |tag| lines.concat(WorkflowTypes.wrap(tag, " # ", " # ")) }
|
|
261
|
+
lines
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def connection_tag
|
|
265
|
+
"@param connection [String, #qube_id] the connection's QuBe Sync id, or a QubeRails::Connection"
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def run_tags
|
|
269
|
+
[
|
|
270
|
+
"@param reference [String] your reference for the run, also its Idempotency-Key, so a retry never starts a second run",
|
|
271
|
+
"@param webhook_url [String] where QuBe Sync sends the run's webhooks",
|
|
272
|
+
"@param version [Integer, nil] the version to run; nil runs the published one",
|
|
273
|
+
"@param client [QubeSync::Client] defaults to #{@client.delete_prefix("::")}"
|
|
274
|
+
]
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def return_tags
|
|
278
|
+
["@return [Hash] the run, as QubeSync::Client#start_run returns it"]
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def input_tags(params)
|
|
282
|
+
params.map do |param|
|
|
283
|
+
types = yard_types(param.input.schema)
|
|
284
|
+
types << "nil" unless param.input.required || types.include?("nil")
|
|
285
|
+
text = input_text(param.input.schema)
|
|
286
|
+
text = "the `#{param.input.name}` input. #{text}" unless param.name == param.input.name
|
|
287
|
+
"@param #{param.name} [#{types.join(", ")}] #{text}".rstrip
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def see_tags(inputs)
|
|
292
|
+
inputs.filter_map { |input| (id = Workflow.operation(input.schema)) && Workflow.known_operation(id) }
|
|
293
|
+
.uniq.sort.map { |entity, action| "@see QubeSync::Operations::#{entity}##{action}" }
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def input_text(schema)
|
|
297
|
+
parts = []
|
|
298
|
+
id = Workflow.operation(schema)
|
|
299
|
+
parts << operation_phrase(id) if id
|
|
300
|
+
description = schema["description"].to_s.strip
|
|
301
|
+
parts[-1] = "#{parts[-1]}:" if id && !description.empty?
|
|
302
|
+
parts << description unless description.empty?
|
|
303
|
+
set = id ? Workflow.set_by_workflow(schema) : []
|
|
304
|
+
unless set.empty?
|
|
305
|
+
parts << "The workflow sets #{set.map { |field| "`#{field}`" }.join(", ")} itself: a value given for " \
|
|
306
|
+
"#{set.size == 1 ? "it" : "them"} is overwritten."
|
|
307
|
+
end
|
|
308
|
+
parts << "One of #{schema["enum"].map(&:inspect).join(", ")}." if schema["enum"].is_a?(Array)
|
|
309
|
+
parts << "Always #{schema["const"].inspect}." if schema.key?("const")
|
|
310
|
+
extra = schema["additionalProperties"]
|
|
311
|
+
if extra.is_a?(Hash) && extra["enum"].is_a?(Array)
|
|
312
|
+
parts << "Each value is one of #{extra["enum"].map(&:inspect).join(", ")}."
|
|
313
|
+
end
|
|
314
|
+
nested = schema["items"].is_a?(Hash) ? schema["items"] : schema
|
|
315
|
+
parts << branches_phrase(nested, nested.equal?(schema) ? "It's" : "Each is") if !id && Workflow.discriminated?(nested)
|
|
316
|
+
parts.join(" ")
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def operation_phrase(id)
|
|
320
|
+
known = Workflow.known_operation(id)
|
|
321
|
+
if known
|
|
322
|
+
"the #{id} body (see QubeSync::Operations::#{known[0]}##{known[1]})"
|
|
323
|
+
else
|
|
324
|
+
"the #{id} body (an operation this version of qube_sync doesn't know)"
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# A nested discriminated object's branches: `"customer", whose add is the customer.create
|
|
329
|
+
# body (...)`.
|
|
330
|
+
def branches_phrase(schema, subject)
|
|
331
|
+
discriminator = schema.dig("discriminator", "propertyName")
|
|
332
|
+
branches = Workflow.branch_schemas(schema).map do |_title, properties, _required|
|
|
333
|
+
fixed = properties[discriminator] || {}
|
|
334
|
+
value = fixed.key?("const") ? fixed["const"] : Array(fixed["enum"]).first
|
|
335
|
+
bodies = properties.sort.filter_map do |name, sub|
|
|
336
|
+
id = Workflow.operation(sub)
|
|
337
|
+
"`#{name}` is #{operation_phrase(id)}" if id
|
|
338
|
+
end
|
|
339
|
+
bodies.empty? ? value.inspect : "#{value.inspect}, whose #{bodies.join(" and ")}"
|
|
340
|
+
end
|
|
341
|
+
"#{subject} one of these, by `#{discriminator}`: #{branches.join("; ")}."
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def yard_types(schema)
|
|
345
|
+
return ["Hash"] if Workflow.operation(schema)
|
|
346
|
+
|
|
347
|
+
types = Array(schema["type"])
|
|
348
|
+
if types.empty?
|
|
349
|
+
return [literal_type(schema["const"])] if schema.key?("const")
|
|
350
|
+
return schema["enum"].map { |value| literal_type(value) }.uniq if schema["enum"].is_a?(Array)
|
|
351
|
+
return ["Hash"] if schema.key?("properties") || schema.key?("oneOf")
|
|
352
|
+
return ["Array<#{yard_types(schema["items"] || {}).join(", ")}>"] if schema.key?("items")
|
|
353
|
+
|
|
354
|
+
return ["Object"]
|
|
355
|
+
end
|
|
356
|
+
types.map do |type|
|
|
357
|
+
case type
|
|
358
|
+
when "string" then "String"
|
|
359
|
+
when "integer" then "Integer"
|
|
360
|
+
when "number" then "Numeric"
|
|
361
|
+
when "boolean" then "Boolean"
|
|
362
|
+
when "null" then "nil"
|
|
363
|
+
when "array" then "Array<#{yard_types(schema["items"] || {}).join(", ")}>"
|
|
364
|
+
when "object" then hash_type(schema)
|
|
365
|
+
else "Object"
|
|
366
|
+
end
|
|
367
|
+
end.uniq
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def hash_type(schema)
|
|
371
|
+
extra = schema["additionalProperties"]
|
|
372
|
+
if extra.is_a?(Hash) && !schema.key?("properties")
|
|
373
|
+
"Hash{String => #{yard_types(extra).join(", ")}}"
|
|
374
|
+
else
|
|
375
|
+
"Hash"
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
def literal_type(value)
|
|
380
|
+
case value
|
|
381
|
+
when String then "String"
|
|
382
|
+
when Integer then "Integer"
|
|
383
|
+
when Float then "Float"
|
|
384
|
+
when true, false then "Boolean"
|
|
385
|
+
when nil then "nil"
|
|
386
|
+
else "Object"
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
end
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module QubeSync
|
|
4
|
+
module WorkflowTypes
|
|
5
|
+
# One workflow of a types document, as the renderers read it: its names, its inputs and,
|
|
6
|
+
# when its input is discriminated (an object with oneOf and a discriminator at the top),
|
|
7
|
+
# its branches, each with the discriminator fixed.
|
|
8
|
+
class Workflow
|
|
9
|
+
# Workflow keys, input names and branch titles, as QuBe Sync allows them.
|
|
10
|
+
NAME = /\A[a-z][a-z0-9_]{0,63}\z/
|
|
11
|
+
OPERATION = /\A[a-z][a-z0-9_]*\.[a-z][a-z0-9_]*\z/
|
|
12
|
+
|
|
13
|
+
# Public methods every module has: a workflow or branch named after one gets a trailing
|
|
14
|
+
# underscore, as QuickBooks' class entity is client.class_. With the ones the generated
|
|
15
|
+
# code calls on its own modules (raise, binding) and the ones it defines.
|
|
16
|
+
MODULE_METHODS = %w[
|
|
17
|
+
alias_method ancestors attr attr_accessor attr_reader attr_writer autoload class class_eval class_exec
|
|
18
|
+
class_variable_get class_variable_set class_variables clone const_get const_missing const_set
|
|
19
|
+
const_source_location constants define_method define_singleton_method deprecate_constant display dup
|
|
20
|
+
enum_for extend freeze hash include included_modules inspect instance_eval instance_exec instance_method
|
|
21
|
+
instance_methods instance_variable_get instance_variable_set instance_variables itself method methods
|
|
22
|
+
module_eval module_exec name object_id prepend private_class_method private_constant
|
|
23
|
+
private_instance_methods private_methods protected_instance_methods protected_methods public_class_method
|
|
24
|
+
public_constant public_instance_method public_instance_methods public_method public_methods public_send
|
|
25
|
+
refinements remove_class_variable remove_instance_variable remove_method send set_temporary_name
|
|
26
|
+
singleton_class singleton_method singleton_methods tap then to_enum to_s undef_method
|
|
27
|
+
undefined_instance_methods yield_self
|
|
28
|
+
binding raise client client= connection_id
|
|
29
|
+
].freeze
|
|
30
|
+
|
|
31
|
+
# name: the input's name; schema: its JSON Schema; required: whether the input must be given.
|
|
32
|
+
Input = Struct.new(:name, :schema, :required, keyword_init: true)
|
|
33
|
+
|
|
34
|
+
# title: the branch's name in the document; method: its start method's name; value: the
|
|
35
|
+
# discriminator's const; inputs: the others, sorted by name; body: the workflow's one
|
|
36
|
+
# operation-body input (a $operation in at least one branch), which every branch's methods
|
|
37
|
+
# take positionally, or nil when the workflow has none or several.
|
|
38
|
+
Branch = Struct.new(:title, :method, :value, :inputs, :body, keyword_init: true)
|
|
39
|
+
|
|
40
|
+
attr_reader :key, :const, :accessor, :name, :description, :version, :draft, :schema, :discriminator,
|
|
41
|
+
:inputs, :branches
|
|
42
|
+
|
|
43
|
+
def initialize(key, entry)
|
|
44
|
+
@key = checked_name(key, "workflow key")
|
|
45
|
+
@const = key.split("_").map(&:capitalize).join
|
|
46
|
+
@accessor = MODULE_METHODS.include?(key) ? "#{key}_" : key
|
|
47
|
+
@name = entry["name"].to_s
|
|
48
|
+
@description = entry["description"].to_s
|
|
49
|
+
@version = entry["version"].is_a?(Integer) ? entry["version"] : nil
|
|
50
|
+
@draft = entry["draft"] == true
|
|
51
|
+
@schema = entry["input"].is_a?(Hash) ? entry["input"] : {}
|
|
52
|
+
@discriminator = schema.dig("discriminator", "propertyName") if Array(schema["oneOf"]).any?
|
|
53
|
+
if @discriminator
|
|
54
|
+
checked_name(@discriminator, "#{key}'s discriminator")
|
|
55
|
+
@inputs = []
|
|
56
|
+
# The inputs that are an operation body in at least one branch. Only when there's one is
|
|
57
|
+
# it positional, in every branch, so every branch's method takes the same shape.
|
|
58
|
+
bodies = schema["oneOf"].flat_map do |branch|
|
|
59
|
+
(branch["properties"] || {}).select { |_name, sub| self.class.operation(sub) }.keys
|
|
60
|
+
end.uniq
|
|
61
|
+
bodies |= (schema["properties"] || {}).select { |_name, sub| self.class.operation(sub) }.keys
|
|
62
|
+
@body_name = bodies.size == 1 ? bodies.first : nil
|
|
63
|
+
@branches = schema["oneOf"].map { |branch| branch_for(branch) }.sort_by(&:method)
|
|
64
|
+
methods = @branches.flat_map { |branch| [branch.method, "#{branch.method}_input"] }
|
|
65
|
+
duplicate = methods.tally.find { |_method, count| count > 1 }
|
|
66
|
+
raise Error, "#{key}'s branches give two methods named #{duplicate.first}" if duplicate
|
|
67
|
+
else
|
|
68
|
+
@inputs = inputs_of(schema["properties"], schema["required"])
|
|
69
|
+
@branches = []
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def discriminated?
|
|
74
|
+
!@discriminator.nil?
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# A $operation reference's id, or nil when schema isn't one.
|
|
78
|
+
def self.operation(schema)
|
|
79
|
+
id = schema.is_a?(Hash) ? schema["$operation"] : nil
|
|
80
|
+
return nil unless id.is_a?(String)
|
|
81
|
+
raise Error, "not an operation id: #{id.inspect}" unless id.match?(OPERATION)
|
|
82
|
+
|
|
83
|
+
id
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# An operation id's entry in this gem's QubeSync::Operations, as [entity class name,
|
|
87
|
+
# action], or nil for one it doesn't know.
|
|
88
|
+
def self.known_operation(id)
|
|
89
|
+
entry = Operations::INDEX[id]
|
|
90
|
+
return nil unless entry
|
|
91
|
+
|
|
92
|
+
[Operations::ENTITIES.fetch(entry[:entity]).to_s, id.split(".", 2).last]
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The fields a $operation reference says its workflow sets itself (`set_by_workflow`).
|
|
96
|
+
def self.set_by_workflow(schema)
|
|
97
|
+
Array(schema["set_by_workflow"]).select { |field| field.is_a?(String) }.uniq.sort
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# An operation's top-level params, {field => [RBS type, required]}, or nil for one this gem
|
|
101
|
+
# doesn't know. The index loads on first use.
|
|
102
|
+
def self.operation_fields(id)
|
|
103
|
+
require_relative "../operations/fields"
|
|
104
|
+
Operations::FIELDS[id]
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# An object schema discriminated by one of its properties: its oneOf branches.
|
|
108
|
+
def self.discriminated?(schema)
|
|
109
|
+
schema.is_a?(Hash) && Array(schema["oneOf"]).any? && schema.dig("discriminator", "propertyName").is_a?(String)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# A discriminated object's branches, as [title, properties merged over the base's,
|
|
113
|
+
# required], sorted by title.
|
|
114
|
+
def self.branch_schemas(schema)
|
|
115
|
+
base = schema["properties"] || {}
|
|
116
|
+
schema["oneOf"].map do |branch|
|
|
117
|
+
[branch["title"].to_s, base.merge(branch["properties"] || {}),
|
|
118
|
+
(Array(schema["required"]) | Array(branch["required"])).sort]
|
|
119
|
+
end.sort_by(&:first)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def branch_for(branch)
|
|
125
|
+
title = checked_name(branch["title"], "a branch title in #{key}")
|
|
126
|
+
properties = (schema["properties"] || {}).merge(branch["properties"] || {})
|
|
127
|
+
fixed = properties[@discriminator] || {}
|
|
128
|
+
value = fixed.key?("const") ? fixed["const"] : Array(fixed["enum"]).first
|
|
129
|
+
raise Error, "#{key}'s #{title} branch doesn't fix #{@discriminator} to a value" if value.nil?
|
|
130
|
+
|
|
131
|
+
required = Array(schema["required"]) | Array(branch["required"])
|
|
132
|
+
inputs = inputs_of(properties.reject { |name, _| name == @discriminator }, required)
|
|
133
|
+
body = inputs.find { |input| input.name == @body_name }
|
|
134
|
+
method = (MODULE_METHODS.include?(title) || title == "start") ? "#{title}_" : title
|
|
135
|
+
Branch.new(title: title, method: method, value: value, inputs: inputs, body: body)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def inputs_of(properties, required)
|
|
139
|
+
required = Array(required)
|
|
140
|
+
(properties || {}).sort.map do |name, sub|
|
|
141
|
+
Input.new(name: checked_name(name, "an input of #{key}"), schema: sub.is_a?(Hash) ? sub : {},
|
|
142
|
+
required: required.include?(name))
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def checked_name(name, what)
|
|
147
|
+
raise Error, "#{what} isn't a name QuBe Sync allows: #{name.inspect}" unless name.is_a?(String) && name.match?(NAME)
|
|
148
|
+
|
|
149
|
+
name
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|