Class: Subroutine::Op

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, ActiveModel::Validations::Callbacks, Fields, Outputs
Defined in:
lib/subroutine/op.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Outputs

#get_output, #output, #output_provided?, #setup_outputs, #validate_outputs!

Methods included from Fields

action_controller_params_loaded?, #all_default_params, #all_params, #all_params_with_defaults, allowed_input_classes, #clear_field, #field_provided?, #fields_in_group, #get_field, #get_field_config, #get_param_group, #original_params, #param_groups, #set_field, #setup_fields, #ungrouped_defaults, #ungrouped_fields, #ungrouped_params, #ungrouped_params_with_defaults

Constructor Details

#initialize(inputs = {}) {|_self| ... } ⇒ Op

Returns a new instance of Op.

Yields:

  • (_self)

Yield Parameters:



45
46
47
48
49
# File 'lib/subroutine/op.rb', line 45

def initialize(inputs = {})
  setup_fields(inputs)
  setup_outputs
  yield self if block_given?
end

Class Method Details

.failure_class(klass) ⇒ Object



19
20
21
# File 'lib/subroutine/op.rb', line 19

def failure_class(klass)
  self._failure_class = klass
end

.submit(*args) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
# File 'lib/subroutine/op.rb', line 32

def submit(*args)
  raise ArgumentError, "Blocks cannot be provided to `submit`." if block_given?

  op = new(*args)
  op.submit
  op
end

.submit!(*args) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/subroutine/op.rb', line 23

def submit!(*args)
  raise ArgumentError, "Blocks cannot be provided to `submit!`" if block_given?

  op = new(*args)
  op.submit!

  op
end

Instance Method Details

#submitObject

the action which should be invoked upon form submission (from the controller)



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/subroutine/op.rb', line 75

def submit
  submit!
  true
rescue Exception => e
  if e.respond_to?(:record)
    inherit_errors(e.record) unless e.record == self
    false
  else
    raise
  end
end

#submit!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/subroutine/op.rb', line 51

def submit!
  begin
    observe_submission do
      validate_and_perform
    end
  rescue Exception => e
    if e.respond_to?(:record)
      inherit_errors(e.record) unless e.record == self
      new_e = _failure_class.new(self)
      raise new_e, new_e.message, e.backtrace
    else
      raise
    end
  end

  if errors.empty?
    validate_outputs!
    self
  else
    raise _failure_class, self
  end
end