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, Validate

Constant Summary collapse

Railway =
Pipetree::Railway

Class Method Summary collapse

Class Method Details

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



11
12
13
14
15
# File 'lib/trailblazer/operation/contract.rb', line 11

def self.Build(name:"default", constant:nil, builder: nil)
  step = ->(input, options) { Build.for(input, options, name: name, constant: constant, builder: builder) }

  [ step, name: "contract.build" ]
end

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



3
4
5
6
7
8
# File 'lib/trailblazer/operation/persist.rb', line 3

def self.Persist(method: :save, name: "default")
  path = "contract.#{name}"
  step = ->(input, options) { options[path].send(method) }

  [ step, name: "persist.save" ]
end

.Validate(skip_extract: false, name: "default", representer: false, key: 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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/trailblazer/operation/validate.rb', line 9

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

  return Validate::Call(name: name, representer: representer, params_path: params_path) if skip_extract || representer

  extract_step,  extract_options  = Validate::Extract(key: key, params_path: params_path)
  validate_step, validate_options = Validate::Call(name: name, representer: representer, params_path: params_path)

  pipe = Railway.new # TODO: make nested pipes simpler.
    .add(Railway::Right, Railway.&(extract_step),  extract_options)
    .add(Railway::Right, Railway.&(validate_step), validate_options)

  step = ->(input, options) { pipe.(input, options).first <= Railway::Right }

  [step, name: "contract.#{name}.validate"]
end