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

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
# File 'lib/trailblazer/macro/contract/build.rb', line 22

def self.Build(name: "default", constant: nil, builder: nil)
  contract_path     = :"contract.#{name}"

  build_injections  = {"#{contract_path}.class": ->(*) { constant }} # default to {constant} if not injected.

  input = ->(ctx, **) do
    ctx.to_hash.merge(
      constant: constant,
      name:     contract_path
    )
  end

  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",
    inject: [build_injections],
    input:  input,
    output: [contract_path]
  }
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 = ->(options, **) { options[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), inject: [extract_injections] unless skip_extract# || representer
    step validate, id: "contract.#{name}.call", inject: [validate_injections]
  end

  options = activity.Subprocess(activity)
  options = options.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.
  options = options.merge(activity.Output(:extract_failure) => activity.Track(:failure)) unless skip_extract

  # Halt failure track to End with {contract.name.invalid}.
  options = options.merge(activity.Output(:failure) => activity.End(:invalid_data)) if invalid_data_terminus

  options
end