Class: Trailblazer::V1_1::Operation

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

Defined Under Namespace

Modules: Builder, Callback, Collection, Controller, Deny, Model, Module, Policy, Representer, Resolver, Responder, Setup Classes: InvalidContract

Constant Summary collapse

Dispatch =

TODO: deprecate this module.

Trailblazer::V1_1::Operation::Callback

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!, #assign_params!, #build_model!, #model!, #params!, #setup!, #setup_model!, #setup_params!

Constructor Details

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

Returns a new instance of Operation.



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

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

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

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Class Method Details

.[](*args) ⇒ Object

TODO: remove in 1.2.



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

def [](*args) # TODO: remove in 1.2.
  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/1.1/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/1.1/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/1.1/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/1.1/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/1.1/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



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

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

#errorsObject



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

def errors
  contract.errors
end

#runObject

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



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

def run
  process(@params)

  [valid?, self]
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  @valid
end