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, #valid_output_type?, #validate_outputs!

Methods included from Fields

action_controller_params_loaded?, #all_default_params, #all_params, #all_params_with_default_params, #all_provided_params, allowed_input_classes, #clear_field, #field_provided?, #get_field, #get_field_config, #get_param_group, #include_defaults_in_params?, #original_params, #param_cache, #param_groups, #set_field, #set_param_group_value, #setup_fields

Constructor Details

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

Returns a new instance of Op.

Yields:

  • (_self)

Yield Parameters:



43
44
45
46
47
# File 'lib/subroutine/op.rb', line 43

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

Class Method Details

.failure_class(klass) ⇒ Object



17
18
19
# File 'lib/subroutine/op.rb', line 17

def failure_class(klass)
  self._failure_class = klass
end

.submit(*args) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


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

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

  op = new(*args)
  op.submit!

  op
end

Instance Method Details

#inspectObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/subroutine/op.rb', line 49

def inspect
  values = provided_params.map do |(key, value)|
    "#{key}: #{value.inspect}"
  end
  values.sort!
  values = values.join(", ")

  oid = format('%x', (object_id << 1))
  "#<#{self.class}:0x#{oid} #{values}>"
end

#submitObject

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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/subroutine/op.rb', line 84

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/subroutine/op.rb', line 60

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