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, Controller, 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.



57
58
59
60
61
# File 'lib/trailblazer/operation.rb', line 57

def initialize(options={})
  @valid            = true
  # DISCUSS: use reverse_merge here?
  @raise_on_invalid = options[:raise_on_invalid] || false
end

Instance Attribute Details

#contractObject (readonly)

Returns the value of attribute contract.



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

def contract
  @contract
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.



35
36
37
# File 'lib/trailblazer/operation.rb', line 35

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

.contract(&block) ⇒ Object



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

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

.present(*params) ⇒ Object

Runs #process without validate and returns the form object.



41
42
43
# File 'lib/trailblazer/operation.rb', line 41

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

.run(*params, &block) ⇒ Object

Endpoint behaviour



22
23
24
25
26
27
28
29
30
31
# File 'lib/trailblazer/operation.rb', line 22

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

#present(*params) ⇒ Object



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

def present(*params)
  setup!(*params)

  @contract = contract_for(nil, @model)
  self
end

#run(*params) ⇒ Object

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



64
65
66
67
68
# File 'lib/trailblazer/operation.rb', line 64

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

  [process(*params), valid?].reverse
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  @valid
end