trailblazer-macro 2.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rubocop.yml +16 -0
- data/.rubocop_todo.yml +642 -0
- data/.travis.yml +15 -0
- data/CHANGES.md +3 -0
- data/COMM-LICENSE +91 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +9 -0
- data/README.md +5 -0
- data/Rakefile +13 -0
- data/lib/trailblazer/macro.rb +13 -0
- data/lib/trailblazer/macro/version.rb +5 -0
- data/lib/trailblazer/operation/guard.rb +18 -0
- data/lib/trailblazer/operation/input_output.rb +28 -0
- data/lib/trailblazer/operation/model.rb +52 -0
- data/lib/trailblazer/operation/nested.rb +90 -0
- data/lib/trailblazer/operation/policy.rb +44 -0
- data/lib/trailblazer/operation/pundit.rb +38 -0
- data/lib/trailblazer/operation/rescue.rb +42 -0
- data/lib/trailblazer/operation/wrap.rb +83 -0
- data/test/docs/guard_test.rb +162 -0
- data/test/docs/macro_test.rb +36 -0
- data/test/docs/model_test.rb +75 -0
- data/test/docs/nested_test.rb +113 -0
- data/test/docs/pundit_test.rb +133 -0
- data/test/docs/rescue_test.rb +126 -0
- data/test/docs/wrap_test.rb +274 -0
- data/test/lib/methods.rb +25 -0
- data/test/operation/model_test.rb +54 -0
- data/test/operation/nested_test.rb +293 -0
- data/test/operation/pundit_test.rb +106 -0
- data/test/test_helper.rb +39 -0
- data/trailblazer-macro.gemspec +36 -0
- metadata +243 -0
data/.travis.yml
ADDED
data/CHANGES.md
ADDED
data/COMM-LICENSE
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
END-USER LICENSE AGREEMENT
|
2
|
+
|
3
|
+
------------------------------------------------------------------------------
|
4
|
+
|
5
|
+
IMPORTANT: THIS SOFTWARE END-USER LICENSE AGREEMENT ("EULA") IS A LEGAL AGREEMENT (“Agreement”) BETWEEN YOU (THE CUSTOMER, EITHER AS AN INDIVIDUAL OR, IF PURCHASED OR OTHERWISE ACQUIRED BY OR FOR AN ENTITY, AS AN ENTITY) AND SYNERGY SOFTWARE SOLUTIONS. READ IT CAREFULLY BEFORE COMPLETING THE INSTALLATION PROCESS AND USING TRAILBLAZER ENTERPRISE AND RELATED SOFTWARE COMPONENTS (“SOFTWARE”). IT PROVIDES A LICENSE TO USE THE SOFTWARE AND CONTAINS WARRANTY INFORMATION AND LIABILITY DISCLAIMERS. IN CONSIDERATION OF YOUR INSTALLATION AND USE OF THE SOFTWARE, YOU HEREBY CONFIRM YOUR ACCEPTANCE OF THE SOFTWARE AND AGREE TO BECOME BOUND BY THE TERMS OF THIS AGREEMENT.
|
6
|
+
|
7
|
+
------------------------------------------------------------------------------
|
8
|
+
|
9
|
+
In order to use the Software under this Agreement, you must receive a “Source URL” at the time of purchase, in accordance with the scope of use and other terms specified for each type of Software and as set forth in this Section 1 of this Agreement.
|
10
|
+
|
11
|
+
1. License Grant
|
12
|
+
|
13
|
+
1.1 General Use. This Agreement grants you a non-exclusive, non-transferable, limited license to the use rights for the Software, without the right to grant sublicenses, subject to the terms and conditions in this Agreement. The Software is licensed, not sold.
|
14
|
+
|
15
|
+
1.2 Limited Organization License. If you purchased an Organization License (included with the Trailblazer Enterprise Software), you may install the Software on an unlimited number of Hosts. “Host” means any physical or virtual machine which is controlled by you.
|
16
|
+
|
17
|
+
1.3 Appliance License. If you purchased an Appliance License, you may distribute the Software in any applications, frameworks, or elements (collectively referred to as an “Application” or “Applications”) that you develop using the Software in accordance with this EULA, provided that such distribution does not violate the restrictions set forth in section 3 of this EULA. You must not remove, obscure or interfere with any copyright, acknowledgment, attribution, trademark, warning or disclaimer statement affixed to, incorporated in or otherwise applied in connection with the Software. You are required to ensure that the Software is not reused by or with any applications other than those with which you distribute it as permitted herein. For example, if You install the Software on a customer’s server, that customer is not permitted to use the Software independently of your Application. You must inform Synergy Software Solutions of your knowledge of any infringing use of the Software by any of your customers. You are liable for compliance by those third parties with the terms and conditions of this EULA. You will not owe Synergy Software Solutions any royalties for your distribution of the Software in accordance with this EULA.
|
18
|
+
|
19
|
+
1.4 Archive Copies. You are entitled to make a reasonable amount of copies of the Software for archival purposes. Each copy must reproduce all copyright and other proprietary rights notices on or in the Software Product.
|
20
|
+
|
21
|
+
1.5 Electronic Delivery. All Software and license documentation shall be delivered by electronic means unless otherwise specified on the applicable invoice (“Invoice”) or at the time of purchase. Software shall be deemed delivered when it is made available for download by you (“Delivery”).
|
22
|
+
|
23
|
+
2. Modifications. Synergy Software Solutions shall provide you with source code so that you can create Modifications of the Software. “Modification” means: (a) any addition to or deletion from the contents of a file included in the original Software or previous Modifications created by You, or (b) any new file that contains any part of the original Software or previous Modifications. While you retain all rights to any original work authored by you as part of the Modifications, We continue to own all copyright and other intellectual property rights in the Software.
|
24
|
+
|
25
|
+
3. Restricted Uses.
|
26
|
+
|
27
|
+
3.1 You shall not (and shall not allow any third party to): (a) decompile, disassemble, or otherwise reverse engineer the Software or attempt to reconstruct or discover any source code, underlying ideas, algorithms, file formats or programming interfaces of the Software by any means whatsoever (except and only to the extent that applicable law prohibits or restricts reverse engineering restrictions); (b) distribute, sell, sublicense, rent, lease or use the Software for time sharing, hosting, service provider or like purposes, except as expressly permitted under this Agreement; (c) redistribute the Software or Modifications other than by including the Software or a portion thereof within your own product, which must have substantially different functionality than the Software or Modifications and must not allow any third party to use the Software or Modifications, or any portions thereof, for software development or application development purposes; (d) redistribute the Software as part of an "appliance" or "virtual server"; (e) redistribute the Software on any server which is not directly under your control; (f) remove any product identification, proprietary, copyright or other notices contained in the Software; (g) modify any part of the Software, create a derivative work of any part of the Software (except as permitted in Section 2), or incorporate the Software, except to the extent expressly authorized in writing by Synergy Software Solutions; (h) publicly disseminate performance information or analysis (including, without limitation, benchmarks) from any source relating to the Software; (i) utilize any equipment, device, software, or other means designed to circumvent or remove any form of Source URL or copy protection used by Synergy Software Solutions in connection with the Software, or use the Software together with any authorization code, Source URL, serial number, or other copy protection device not supplied by Synergy Software Solutions; (j) use the Software to develop a product which is competitive with any Synergy Software Solutions product offerings(unless such development is allowed by applicable law); or (k) use unauthorized Source URLS or keycode(s) or distribute or publish Source URLs or keycode(s), except as may be expressly permitted by Synergy Software Solutions in writing. If your unique Source URL is ever published, Synergy Software Solutions reserves the right to terminate your access without notice.
|
28
|
+
|
29
|
+
3.2 UNDER NO CIRCUMSTANCES MAY YOU USE THE SOFTWARE AS PART OF A PRODUCT OR SERVICE THAT PROVIDES SIMILAR FUNCTIONALITY TO THE SOFTWARE ITSELF.
|
30
|
+
|
31
|
+
The Open Source version of the Software (“LGPL Version”) is licensed
|
32
|
+
under the terms of the GNU Lesser General Public License versions 3.0
|
33
|
+
(“LGPL”) and not under this EULA.
|
34
|
+
|
35
|
+
4. Ownership. Notwithstanding anything to the contrary contained herein, except for the limited license rights expressly provided herein, Synergy Software Solutions and its suppliers have and will retain all rights, title and interest (including, without limitation, all patent, copyright, trademark, trade secret and other intellectual property rights) in and to the Software and all copies, modifications and derivative works thereof (including any changes which incorporate any of your ideas, feedback or suggestions). You acknowledge that you are obtaining only a limited license right to the Software, and that irrespective of any use of the words “purchase”, “sale” or like terms hereunder no ownership rights are being conveyed to you under this Agreement or otherwise.
|
36
|
+
|
37
|
+
5. Fees and Payment. The Software license fees will be due and payable in full as set forth in the Invoice or at the time of purchase. If the Software does not function properly within two weeks of purchase, please contact us within those two weeks for a refund. You shall be responsible for all taxes, withholdings, duties and levies arising from the order (excluding taxes based on the net income of Synergy Software Solutions).
|
38
|
+
|
39
|
+
6. Support, Maintenance and Services. Subject to the terms and conditions of this Agreement, as set forth in the Invoice, and as set forth on the Trailblazer Enterprise support page (http://enterprise.trailblazer.to), support and maintenance services may be included with the purchase of your license subscription.
|
40
|
+
|
41
|
+
7. Term of Agreement.
|
42
|
+
|
43
|
+
7.1 Term. This Agreement is effective as of the Delivery of the Software and expires at such time as all license and service subscriptions hereunder have expired in accordance with their own terms (the “Term”). For clarification, the term of your license under this Agreement may be perpetual, limited for Evaluation Version, or designated as a fixed-term license in the Invoice, and shall be specified at your time of purchase. Either party may terminate this Agreement (including all related Invoices) if the other party: (a) fails to cure any material breach of this Agreement within thirty (30) days after written notice of such breach, provided that Synergy Software Solutions may terminate this Agreement immediately upon any breach of Section 3 or if you exceed any other restrictions contained in Section 1, unless otherwise specified in this agreement; (b) ceases operation without a successor; or (c) seeks protection under any bankruptcy, receivership, trust deed, creditors arrangement, composition or comparable proceeding, or if any such proceeding is instituted against such party (and not dismissed within sixty (60) days)). Termination is not an exclusive remedy and the exercise by either party of any remedy under this Agreement will be without prejudice to any other remedies it may have under this Agreement, by law, or otherwise.
|
44
|
+
|
45
|
+
7.2 Termination. Upon any termination of this Agreement, you shall cease any and all use of any Software and destroy all copies thereof.
|
46
|
+
|
47
|
+
7.3 Expiration of License. Upon the expiration of any term under this Agreement, (a) all Software updates and services pursuant to the license shall cease, (b) you may only continue to run existing installations of the Software, (c) you may not install the Software on any additional Hosts, and (d) any new installation of the Software shall require the purchase of a new license subscription from Synergy Software Solutions.
|
48
|
+
|
49
|
+
8. Disclaimer of Warranties. The Software is provided "as is," with all faults, defects and errors, and without warranty of any kind. Synergy Software Solutions does not warrant that the Software will be free of bugs, errors, viruses or other defects, and Synergy Software Solutions shall have no liability of any kind for the use of or inability to use the Software, the Software content or any associated service, and you acknowledge that it is not technically practicable for Synergy Software Solutions to do so.
|
50
|
+
To the maximum extent permitted by applicable law, Synergy Software Solutions disclaims all warranties, express, implied, arising by law or otherwise, regarding the Software, the Software content and their respective performance or suitability for your intended use, including without limitation any implied warranty of merchantability, fitness for a particular purpose.
|
51
|
+
|
52
|
+
9. Limitation of Liability.
|
53
|
+
|
54
|
+
To the maximum extent permitted by applicable law, Synergy Software Solutions will not be liable for any direct, indirect, consequential, incidental, special, exemplary, or punitive damages or liabilities whatsoever arising from or relating to the Software, the Software content or this Agreement, whether based on contract, tort (including negligence), strict liability or other theory, even if Synergy Software Solutions has been advised of the possibility of such damages.
|
55
|
+
|
56
|
+
In no event will Synergy Software Solutions' liability exceed the Software license price as indicated in the Invoice. The existence of more than one claim will not enlarge or extend this limit.
|
57
|
+
|
58
|
+
10. Remedies. Your exclusive remedy and Synergy Software Solutions’ entire liability for breach of this Agreement shall be limited, at Synergy Software Solutions’ sole and exclusive discretion, to (a) replacement of any defective software or documentation; or (b) refund of the license fee paid to Synergy Software Solutions, payable in accordance with Synergy Software Solutions' refund policy.
|
59
|
+
|
60
|
+
11. Acknowledgements.
|
61
|
+
|
62
|
+
Consent to the Use of Data. You agree that Synergy Software Solutions and its affiliates may collect and use technical information gathered as part of the product support services. Synergy Software Solutions may use this information solely to improve products and services and will not disclose this information in a form that personally identifies you.
|
63
|
+
|
64
|
+
12. Third Party Software. Examples included in Software may provide links to third party libraries or code (collectively “Third Party Software”) to implement various functions. Third Party Software does not comprise part of the Software. In some cases, access to Third Party Software may be included along with the Software Delivery as a convenience for demonstration purposes. Such source code and libraries may be included in the “…/examples” source tree delivered with the Software and do not comprise the Software. Licensee acknowledges (1) that some part of Third Party Software may require additional licensing of copyright and patents from the owners of such, and (2) that distribution of any of the Software referencing or including any portion of a Third Party Software may require appropriate licensing from such third parties.
|
65
|
+
|
66
|
+
|
67
|
+
13. Miscellaneous
|
68
|
+
|
69
|
+
13.1 Entire Agreement. This Agreement sets forth our entire agreement with respect to the Software and the subject matter hereof and supersedes all prior and contemporaneous understandings and agreements whether written or oral.
|
70
|
+
|
71
|
+
13.2 Amendment. Synergy Software Solutions reserves the right, in its sole discretion, to amend this Agreement from time to time. Amendments to this Agreement can be located at: https://github.com/trailblazer/trailblazer/blob/master/COMM-LICENSE.
|
72
|
+
|
73
|
+
13.3 Assignment. You may not assign this Agreement or any of its rights under this Agreement without the prior written consent of Synergy Software Solutions and any attempted assignment without such consent shall be void.
|
74
|
+
|
75
|
+
13.4 Export Compliance. You agree to comply with all applicable laws and regulations, including laws, regulations, orders or other restrictions on export, re-export or redistribution of software.
|
76
|
+
|
77
|
+
13.5 Indemnification. You agree to defend, indemnify, and hold harmless Synergy Software Solutions from and against any lawsuits, claims, losses, damages, fines and expenses (including attorneys' fees and costs) arising out of your use of the Software or breach of this Agreement.
|
78
|
+
|
79
|
+
13.6 Governing Law. This Agreement is governed and construed in accordance with the law of Singapore and shall be subject to the exclusive jurisdiction of the Singapore Courts. The operation of the United Nations Convention on the International Sale of Goods is hereby expressly excluded.
|
80
|
+
|
81
|
+
13.7 Dispute Resolution. In the event of any dispute arising from or in connection with this Agreement, the parties undertake to make all their best efforts to settle the dispute amicably through negotiations, failing which either party may submit the dispute for resolution by the Singapore Courts.
|
82
|
+
|
83
|
+
13.8 Attorneys’ Fees and Costs. The prevailing party in any action to enforce this Agreement will be entitled to recover its attorneys’ fees and costs in connection with such action.
|
84
|
+
|
85
|
+
13.9 Severability. If any provision of this Agreement is held by a court of competent jurisdiction to be invalid, illegal, or unenforceable, the remainder of this Agreement will remain in full force and effect.
|
86
|
+
|
87
|
+
13.10 Waiver. Failure or neglect by either party to enforce at any time any of the provisions of this licence Agreement shall not be construed or deemed to be a waiver of that party's rights under this Agreement.
|
88
|
+
|
89
|
+
13.11 Headings. The headings of sections and paragraphs of this Agreement are for convenience of reference only and are not intended to restrict, affect or be of any weight in the interpretation or construction of the provisions of such sections or paragraphs.
|
90
|
+
|
91
|
+
14. Contact Information. If you have any questions about this EULA, or if you want to contact Synergy Software Solutions for any reason, please direct correspondence to info@trailblazer.to.
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in trailblazer.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# gem "trailblazer", github: "trailblazer/trailblazer"
|
7
|
+
# gem "trailblazer-operation", path: "../operation"
|
8
|
+
# gem "trailblazer-operation", github: "trailblazer/trailblazer-operation"
|
9
|
+
gem "trailblazer-activity"#, github: "trailblazer/trailblazer-activity"
|
10
|
+
gem "trailblazer-macro-contract", git: "https://github.com/trailblazer/trailblazer-macro-contract"
|
11
|
+
|
12
|
+
gem "minitest-line"
|
13
|
+
gem "rubocop", require: false
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Copyright (c) Nick Sutterer
|
2
|
+
|
3
|
+
Trailblazer is an Open Source project licensed under the terms of
|
4
|
+
the LGPLv3 license. Please see <http://www.gnu.org/licenses/lgpl-3.0.html>
|
5
|
+
for license text.
|
6
|
+
|
7
|
+
Trailblazer Enterprise has a commercial-friendly license allowing private forks
|
8
|
+
and modifications of Trailblazer. Please see http://trailblazer.to/enterprise/ for
|
9
|
+
more detail. You can find the commercial license terms in COMM-LICENSE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
require "rubocop/rake_task"
|
4
|
+
|
5
|
+
task :default => [:test]
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |test|
|
8
|
+
test.libs << 'test'
|
9
|
+
test.test_files = FileList['test/**/*_test.rb']
|
10
|
+
test.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
RuboCop::RakeTask.new
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "trailblazer/macro/version"
|
2
|
+
require "trailblazer/operation"
|
3
|
+
|
4
|
+
require "trailblazer/operation/inject"
|
5
|
+
require "trailblazer/operation/input_output"
|
6
|
+
|
7
|
+
require "trailblazer/operation/model"
|
8
|
+
require "trailblazer/operation/policy"
|
9
|
+
require "trailblazer/operation/guard"
|
10
|
+
require "trailblazer/operation/pundit"
|
11
|
+
require "trailblazer/operation/nested"
|
12
|
+
require "trailblazer/operation/rescue"
|
13
|
+
require "trailblazer/operation/wrap"
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Trailblazer::Operation
|
2
|
+
module Policy
|
3
|
+
def self.Guard(proc, name: :default, &block)
|
4
|
+
Policy.step( Guard.build(proc), name: name )
|
5
|
+
end
|
6
|
+
|
7
|
+
module Guard
|
8
|
+
def self.build(callable)
|
9
|
+
option = Trailblazer::Option::KW(callable)
|
10
|
+
|
11
|
+
# this gets wrapped in a Operation::Result object.
|
12
|
+
->( (options, *), circuit_args ) do
|
13
|
+
Result.new(!!option.call(options, circuit_args), {})
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Trailblazer
|
2
|
+
# Add an input and output filter for a task, allowing to control what a task "sees"
|
3
|
+
# (receives as input) and returns (or, what the outer caller "sees").
|
4
|
+
#
|
5
|
+
# This works by adding two variable mappers to the taskWrap.
|
6
|
+
# One before the actual task gets called (input) and one before the end (output).
|
7
|
+
module Operation::InputOutput
|
8
|
+
# naming: Macaroni, VariableMapping
|
9
|
+
def self.plan(input, output)
|
10
|
+
default_input_filter = ->(options, *) { ctx = options }
|
11
|
+
default_output_filter = ->(options, *) { options }
|
12
|
+
|
13
|
+
input ||= default_input_filter
|
14
|
+
output ||= default_output_filter
|
15
|
+
|
16
|
+
input_filter = Activity::TaskWrap::Input.new(input)
|
17
|
+
output_filter = Activity::TaskWrap::Output.new(output)
|
18
|
+
|
19
|
+
# taskWrap extensions
|
20
|
+
Module.new do
|
21
|
+
extend Activity::Path::Plan()
|
22
|
+
|
23
|
+
task input_filter, id: ".input", before: "task_wrap.call_task"
|
24
|
+
task output_filter, id: ".output", before: "End.success", group: :end # DISCUSS: position
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class Trailblazer::Operation
|
2
|
+
def self.Model(model_class, action=nil)
|
3
|
+
task = Trailblazer::Activity::TaskBuilder::Binary.call(Model.new)
|
4
|
+
|
5
|
+
extension = Trailblazer::Activity::TaskWrap::Merge.new(
|
6
|
+
Wrap::Inject::Defaults(
|
7
|
+
"model.class" => model_class,
|
8
|
+
"model.action" => action
|
9
|
+
)
|
10
|
+
)
|
11
|
+
|
12
|
+
{ task: task, id: "model.build", extension: [extension] }
|
13
|
+
end
|
14
|
+
|
15
|
+
class Model
|
16
|
+
def call(options, params:, **)
|
17
|
+
builder = Model::Builder.new
|
18
|
+
options[:model] = model = builder.call(options, params)
|
19
|
+
options["result.model"] = result = Result.new(!model.nil?, {})
|
20
|
+
|
21
|
+
result.success?
|
22
|
+
end
|
23
|
+
|
24
|
+
class Builder
|
25
|
+
def call(options, params)
|
26
|
+
action = options["model.action"] || :new
|
27
|
+
model_class = options["model.class"]
|
28
|
+
action = :pass_through unless %i[new find_by find].include?(action)
|
29
|
+
|
30
|
+
send("#{action}!", model_class, params, options["model.action"])
|
31
|
+
end
|
32
|
+
|
33
|
+
def new!(model_class, params, *)
|
34
|
+
model_class.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def find!(model_class, params, *)
|
38
|
+
model_class.find(params[:id])
|
39
|
+
end
|
40
|
+
|
41
|
+
# Doesn't throw an exception and will return false to divert to Left.
|
42
|
+
def find_by!(model_class, params, *)
|
43
|
+
model_class.find_by(id: params[:id])
|
44
|
+
end
|
45
|
+
|
46
|
+
# Call any method on the model class and pass :id.
|
47
|
+
def pass_through!(model_class, params, action)
|
48
|
+
model_class.send(action, params[:id])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# per default, everything we pass into a circuit is immutable. it's the ops/act's job to allow writing (via a Context)
|
2
|
+
module Trailblazer
|
3
|
+
class Operation
|
4
|
+
# {Nested} macro.
|
5
|
+
def self.Nested(callable, input:nil, output:nil, id: "Nested(#{callable})")
|
6
|
+
task_wrap_extensions = Module.new do
|
7
|
+
extend Activity::Path::Plan()
|
8
|
+
end
|
9
|
+
|
10
|
+
task, operation, is_dynamic = Nested.build(callable)
|
11
|
+
|
12
|
+
# TODO: move this to the generic step DSL
|
13
|
+
if input || output
|
14
|
+
task_wrap_extensions = InputOutput.plan( input, output )
|
15
|
+
end
|
16
|
+
|
17
|
+
if is_dynamic
|
18
|
+
task_wrap_extensions.task task.method(:compute_nested_activity), id: ".compute_nested_activity", after: "Start.default", group: :start
|
19
|
+
task_wrap_extensions.task task.method(:compute_return_signal), id: ".compute_return_signal", after: "task_wrap.call_task"
|
20
|
+
end
|
21
|
+
|
22
|
+
{
|
23
|
+
task: task,
|
24
|
+
id: id,
|
25
|
+
extension: [Trailblazer::Activity::TaskWrap::Merge.new(task_wrap_extensions)],
|
26
|
+
outputs: operation.outputs
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
# @private
|
31
|
+
module Nested
|
32
|
+
# DISCUSS: use builders here?
|
33
|
+
def self.build(nested_operation)
|
34
|
+
return dynamic = Dynamic.new(nested_operation), dynamic, true unless nestable_object?(nested_operation)
|
35
|
+
|
36
|
+
# The returned {Nested} instance is a valid circuit element and will be `call`ed in the circuit.
|
37
|
+
# It simply returns the nested activity's `signal,options,flow_options` return set.
|
38
|
+
# The actual wiring - where to go with that - is done by the step DSL.
|
39
|
+
return Trailblazer::Operation::Callable(nested_operation, call: :__call__), nested_operation, false
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.nestable_object?(object)
|
43
|
+
object.is_a?( Trailblazer::Activity::Interface )
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.operation_class
|
47
|
+
Operation
|
48
|
+
end
|
49
|
+
|
50
|
+
# For dynamic `Nested`s that do not expose an {Activity} interface.
|
51
|
+
# Since we do not know its outputs, we have to map them to :success and :failure, only.
|
52
|
+
#
|
53
|
+
# This is what {Nested} in 2.0 used to do, where the outcome could only be true/false (or success/failure).
|
54
|
+
class Dynamic
|
55
|
+
def initialize(nested_activity)
|
56
|
+
@nested_activity = Trailblazer::Option::KW(nested_activity)
|
57
|
+
@outputs = {
|
58
|
+
:success => Activity::Output( Railway::End::Success.new(semantic: :success), :success ),
|
59
|
+
:failure => Activity::Output( Railway::End::Failure.new(semantic: :failure), :failure ),
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
attr_reader :outputs
|
64
|
+
|
65
|
+
# TaskWrap step.
|
66
|
+
def compute_nested_activity((wrap_ctx, original_args), **circuit_options)
|
67
|
+
(ctx,), original_circuit_options = original_args
|
68
|
+
|
69
|
+
# TODO: evaluate the option to get the actual "object" to call.
|
70
|
+
activity = @nested_activity.call(ctx, original_circuit_options)
|
71
|
+
|
72
|
+
# Overwrite :task so task_wrap.call_task will call this activity.
|
73
|
+
# This is a trick so we don't have to repeat logic from #call_task here.
|
74
|
+
wrap_ctx[:task] = Trailblazer::Operation::Callable( activity, call: :__call__ )
|
75
|
+
|
76
|
+
return Activity::Right, [wrap_ctx, original_args]
|
77
|
+
end
|
78
|
+
|
79
|
+
def compute_return_signal((wrap_ctx, original_args), **circuit_options)
|
80
|
+
# Translate the genuine nested signal to the generic Dynamic end (success/failure, only).
|
81
|
+
# Note that here we lose information about what specific event was emitted.
|
82
|
+
wrap_ctx[:return_signal] = wrap_ctx[:return_signal].kind_of?(Railway::End::Success) ?
|
83
|
+
@outputs[:success].signal : @outputs[:failure].signal
|
84
|
+
|
85
|
+
return Activity::Right, [wrap_ctx, original_args]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Trailblazer::Operation
|
2
|
+
module Policy
|
3
|
+
# Step: This generically `call`s a policy and then pushes its result to `options`.
|
4
|
+
# You can use any callable object as a policy with this step.
|
5
|
+
class Eval
|
6
|
+
def initialize(name:nil, path:nil)
|
7
|
+
@name = name
|
8
|
+
@path = path
|
9
|
+
end
|
10
|
+
|
11
|
+
# incoming low-level {Task API}.
|
12
|
+
# outgoing Task::Binary API.
|
13
|
+
def call((options, flow_options), **circuit_options)
|
14
|
+
condition = options[ @path ] # this allows dependency injection.
|
15
|
+
result = condition.( [options, flow_options], **circuit_options )
|
16
|
+
|
17
|
+
options["policy.#{@name}"] = result["policy"] # assign the policy as a skill.
|
18
|
+
options["result.policy.#{@name}"] = result
|
19
|
+
|
20
|
+
# flow control
|
21
|
+
signal = result.success? ? Trailblazer::Activity::Right : Trailblazer::Activity::Left # since we & this, it's only executed OnRight and the return boolean decides the direction, input is passed straight through.
|
22
|
+
|
23
|
+
return signal, [ options, flow_options ]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Adds the `yield` result to the pipe and treats it like a
|
28
|
+
# policy-compatible object at runtime.
|
29
|
+
def self.step(condition, options, &block)
|
30
|
+
name = options[:name]
|
31
|
+
path = "policy.#{name}.eval"
|
32
|
+
|
33
|
+
task = Eval.new( name: name, path: path )
|
34
|
+
|
35
|
+
extension = Trailblazer::Activity::TaskWrap::Merge.new(
|
36
|
+
Trailblazer::Operation::Wrap::Inject::Defaults(
|
37
|
+
path => condition
|
38
|
+
)
|
39
|
+
)
|
40
|
+
|
41
|
+
{ task: task, id: path, extension: [extension] }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Trailblazer::Operation
|
2
|
+
module Policy
|
3
|
+
def self.Pundit(policy_class, action, name: :default)
|
4
|
+
Policy.step( Pundit.build(policy_class, action), name: name )
|
5
|
+
end
|
6
|
+
|
7
|
+
module Pundit
|
8
|
+
def self.build(*args, &block)
|
9
|
+
Condition.new(*args, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Pundit::Condition is invoked at runtime when iterating the pipe.
|
13
|
+
class Condition
|
14
|
+
def initialize(policy_class, action)
|
15
|
+
@policy_class, @action = policy_class, action
|
16
|
+
end
|
17
|
+
|
18
|
+
# Instantiate the actual policy object, and call it.
|
19
|
+
def call((options), *)
|
20
|
+
policy = build_policy(options) # this translates to Pundit interface.
|
21
|
+
result!(policy.send(@action), policy)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def build_policy(options)
|
26
|
+
@policy_class.new(options[:current_user], options[:model])
|
27
|
+
end
|
28
|
+
|
29
|
+
def result!(success, policy)
|
30
|
+
data = { "policy" => policy }
|
31
|
+
data["message"] = "Breach" if !success # TODO: how to allow messages here?
|
32
|
+
|
33
|
+
Result.new(success, data)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|