Class: Trailblazer::Operation

Inherits:
Object
  • Object
show all
Extended by:
Uber::InheritableAttr
Includes:
Uber::Builder
Defined in:
lib/trailblazer/operation.rb,
lib/trailblazer/operation/crud.rb,
lib/trailblazer/operation/worker.rb

Defined Under Namespace

Modules: CRUD, Collection, Controller, Dispatch, Module, Representer, Responder, Worker Classes: InvalidContract, UploadedFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Operation

Returns a new instance of Operation.



60
61
62
63
# File 'lib/trailblazer/operation.rb', line 60

def initialize(options={})
  @valid            = true
  @raise_on_invalid = options[:raise_on_invalid] || false
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Class Method Details

.call(*params) ⇒ Object Also known as: []

::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.



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

def call(*params)
  build_operation_class(*params).new(raise_on_invalid: true).run(*params).last
end

.contract(&block) ⇒ Object



48
49
50
# File 'lib/trailblazer/operation.rb', line 48

def contract(&block)
  contract_class.class_eval(&block)
end

.present(*params) ⇒ Object

Runs #setup! and returns the form object.



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

def present(*params)
  build_operation_class(*params).new.present(*params)
end

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

Like ::run, but yield block when invalid.

Yields:

  • (op)


30
31
32
33
34
# File 'lib/trailblazer/operation.rb', line 30

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

.run(*params, &block) ⇒ Object

Endpoint behaviour



18
19
20
21
22
23
24
25
26
27
# File 'lib/trailblazer/operation.rb', line 18

def run(*params, &block) # Endpoint behaviour
  res, op = build_operation_class(*params).new.run(*params)

  if block_given?
    yield op if res
    return op
  end

  [res, op]
end

Instance Method Details

#contract(*args) ⇒ Object



90
91
92
# File 'lib/trailblazer/operation.rb', line 90

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

#errorsObject



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

def errors
  contract.errors
end

#present(*params) ⇒ Object



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

def present(*params)
  setup!(*params)
  contract!
  self
end

#run(*params) ⇒ Object

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



66
67
68
69
70
71
72
# File 'lib/trailblazer/operation.rb', line 66

def run(*params)
  setup!(*params) # where do we assign/find the model?

  process(*params)

  [valid?, self]
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  @valid
end