Class: Trailblazer::Operation

Inherits:
Object
  • Object
show all
Extended by:
Builder, Uber::InheritableAttr
Includes:
Setup
Defined in:
lib/trailblazer/operation.rb,
lib/trailblazer/operation/model.rb,
lib/trailblazer/operation/resolver.rb,
lib/trailblazer/operation/model/dsl.rb,
lib/trailblazer/operation/model/external.rb

Defined Under Namespace

Modules: Builder, Collection, Controller, Deny, Dispatch, Model, Module, Policy, Representer, Resolver, Setup Classes: InvalidContract, UploadedFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder

builder_class, builder_class=, extended

Methods included from Setup

#assign_model!, #build_model!, #model!, #setup!, #setup_model!, #setup_params!

Constructor Details

#initialize(params, options = {}) ⇒ Operation

Returns a new instance of Operation.



61
62
63
64
65
66
67
# File 'lib/trailblazer/operation.rb', line 61

def initialize(params, options={})
  @params           = params
  @options          = options
  @valid            = true

  setup!(params) # assign/find the model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



76
77
78
# File 'lib/trailblazer/operation.rb', line 76

def model
  @model
end

Class Method Details

.[](*args) ⇒ Object

TODO: remove in 1.1.



37
38
39
40
# File 'lib/trailblazer/operation.rb', line 37

def [](*args) # TODO: remove in 1.1.
  warn "[Trailblazer] Operation[] is deprecated. Please use Operation.() and have a nice day."
  call(*args)
end

.call(params) ⇒ Object

::call only returns the Operation instance (or whatever was returned from #validate). This is useful in tests or in irb, e.g. when using Op as a factory and you already know it’s valid.



33
34
35
# File 'lib/trailblazer/operation.rb', line 33

def call(params)
  build_operation(params, raise_on_invalid: true).run.last
end

.contract(constant = nil, &block) ⇒ Object

This is a DSL method. Use ::contract_class and ::contract_class= for the explicit version.

Op.contract #=> returns contract class
Op.contract do .. end # defines contract
Op.contract CommentForm # copies (and subclasses) external contract.
Op.contract CommentForm do .. end # copies and extends contract.


52
53
54
55
56
57
# File 'lib/trailblazer/operation.rb', line 52

def contract(constant=nil, &block)
  return contract_class unless constant or block_given?

  self.contract_class= Class.new(constant) if constant
  contract_class.class_eval(&block) if block_given?
end

.present(params) ⇒ Object

Runs #setup! but doesn’t process the operation.



43
44
45
# File 'lib/trailblazer/operation.rb', line 43

def present(params)
  build_operation(params)
end

.reject(*args) {|op| ... } ⇒ Object

Like ::run, but yield block when invalid.

Yields:

  • (op)


25
26
27
28
29
# File 'lib/trailblazer/operation.rb', line 25

def reject(*args)
  res, op = run(*args)
  yield op if res == false
  op
end

.run(params, &block) ⇒ Object

Endpoint behaviour



13
14
15
16
17
18
19
20
21
22
# File 'lib/trailblazer/operation.rb', line 13

def run(params, &block) # Endpoint behaviour
  res, op = build_operation(params).run

  if block_given?
    yield op if res
    return op
  end

  [res, op]
end

Instance Method Details

#contract(*args) ⇒ Object



86
87
88
# File 'lib/trailblazer/operation.rb', line 86

def contract(*args)
  contract!(*args)
end

#errorsObject



78
79
80
# File 'lib/trailblazer/operation.rb', line 78

def errors
  contract.errors
end

#runObject

Operation.run(body: “Fabulous!”) #=> [true, <Comment body: “Fabulous!”>]



70
71
72
73
74
# File 'lib/trailblazer/operation.rb', line 70

def run
  process(@params)

  [valid?, self]
end

#valid?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/trailblazer/operation.rb', line 82

def valid?
  @valid
end