Module: Trailblazer::Macro::Contract
- Defined in:
- lib/trailblazer/macro/contract.rb,
lib/trailblazer/macro/contract/build.rb,
lib/trailblazer/macro/contract/persist.rb,
lib/trailblazer/macro/contract/validate.rb
Defined Under Namespace
Modules: DSL Classes: Validate
Class Method Summary collapse
- .Build(name: "default", constant: nil, builder: nil) ⇒ Object
- .Persist(method: :save, name: "default") ⇒ Object
-
.Validate(skip_extract: false, name: "default", representer: false, key: nil, constant: nil, invalid_data_terminus: false) ⇒ Object
result.contract = .. result.contract.errors = .. Deviate to left track if optional key is not found in params.
Class Method Details
.Build(name: "default", constant: nil, builder: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/trailblazer/macro/contract/build.rb', line 22 def self.Build(name: "default", constant: nil, builder: nil) contract_path = :"contract.#{name}" injections = { Activity::Railway.Inject() => { "#{contract_path}.class": ->(*) { constant }, # default to {constant} if not injected. } } # DISCUSS: can we force-default this via Inject()? input = { Activity::Railway.In() => ->(ctx, **) do ctx.to_hash.merge( constant: constant, name: contract_path ) end } output = { Activity::Railway.Out() => [contract_path] } default_contract_builder = ->(ctx, model: nil, **) { ctx[:"#{contract_path}.class"].new(model) } # proc is called via {Option()}. task_option_proc = builder ? builder : default_contract_builder # after the builder proc is run, assign its result to {:"contract.default"}. ctx_assign_block = ->(result, ctx) { ctx[contract_path] = result } task = CircuitTaskWithResultProcessing.new(Trailblazer::Option(task_option_proc), task_option_proc, ctx_assign_block) { task: task, id: "contract.build", }. merge(injections). merge(input). merge(output) end |
.Persist(method: :save, name: "default") ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/trailblazer/macro/contract/persist.rb', line 4 def self.Persist(method: :save, name: "default") path = :"contract.#{name}" step = ->(, **) { [path].send(method) } task = Activity::TaskBuilder::Binary(step) {task: task, id: "persist.save"} end |
.Validate(skip_extract: false, name: "default", representer: false, key: nil, constant: nil, invalid_data_terminus: false) ⇒ Object
result.contract = .. result.contract.errors = .. Deviate to left track if optional key is not found in params. Deviate to left if validation result falsey.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/trailblazer/macro/contract/validate.rb', line 8 def self.Validate(skip_extract: false, name: "default", representer: false, key: nil, constant: nil, invalid_data_terminus: false) # DISCUSS: should we introduce something like Validate::Deserializer? contract_path = :"contract.#{name}" # the contract instance params_path = :"contract.#{name}.params" # extract_params! save extracted params here. key_path = :"contract.#{name}.extract_key" extract = Validate::Extract.new(key_path: key_path, params_path: params_path) validate = Validate.new(name: name, representer: representer, params_path: params_path, contract_path: contract_path) # These are defaulting dependency injection, more here # https://trailblazer.to/2.1/docs/activity.html#activity-dependency-injection-inject-defaulting extract_injections = {key_path => ->(*) { key }} # default to {key} if not injected. validate_injections = {contract_path => ->(*) { constant }} # default the contract instance to {constant}, if not injected (or passed down from {Build()}) # Build a simple Railway {Activity} for the internal flow. activity = Class.new(Activity::Railway(name: "Contract::Validate")) do step extract, id: "#{params_path}_extract", Output(:failure) => End(:extract_failure), Activity::Railway.Inject() => extract_injections unless skip_extract# || representer step validate, id: "contract.#{name}.call", Activity::Railway.Inject() => validate_injections end = activity.Subprocess(activity) = .merge(id: "contract.#{name}.validate") # Deviate End.extract_failure to the standard failure track as a default. This can be changed from the user side. = .merge(activity.Output(:extract_failure) => activity.Track(:failure)) unless skip_extract # Halt failure track to End with {contract.name.invalid}. = .merge(activity.Output(:failure) => activity.End(:invalid_data)) if invalid_data_terminus end |