Module: Trailblazer::Operation::Contract

Defined in:
lib/trailblazer/operation/persist.rb,
lib/trailblazer/operation/contract.rb,
lib/trailblazer/operation/validate.rb

Defined Under Namespace

Modules: Build, DSL Classes: Validate

Class Method Summary collapse

Class Method Details

.Build(name: "default", constant: nil, builder: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/trailblazer/operation/contract.rb', line 4

def self.Build(name: "default", constant: nil, builder: nil)
  task = lambda do |(options, flow_options), **circuit_options|
    result = Build.(options, circuit_options, name: name, constant: constant, builder: builder)

    return Activity::TaskBuilder.binary_signal_for(result, Activity::Right, Activity::Left),
        [options, flow_options]
  end

  {task: task, id: "contract.build"}
end

.Persist(method: :save, name: "default") ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/trailblazer/operation/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) ⇒ 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
# File 'lib/trailblazer/operation/validate.rb', line 8

def self.Validate(skip_extract: false, name: "default", representer: false, key: nil, constant: nil) # DISCUSS: should we introduce something like Validate::Deserializer?
  params_path = :"contract.#{name}.params" # extract_params! save extracted params here.

  extract  = Validate::Extract.new(key: key, params_path: params_path).freeze
  validate = Validate.new(name: name, representer: representer, params_path: params_path, constant: constant).freeze

  # 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) unless skip_extract# || representer
    step validate, id: "contract.#{name}.call"
  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

  options
end