class Subroutine::Op

Public Class Methods

failure_class(klass) click to toggle source
# File lib/subroutine/op.rb, line 19
def failure_class(klass)
  self._failure_class = klass
end
new(inputs = {}) { |self| ... } click to toggle source
# File lib/subroutine/op.rb, line 62
def initialize(inputs = {})
  setup_fields(inputs)
  setup_outputs
  yield self if block_given?
end
submit(*args) click to toggle source
# 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) click to toggle source
# 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

Protected Class Methods

field(field_name, options = {}) click to toggle source
Calls superclass method
# File lib/subroutine/op.rb, line 42
def field(field_name, options = {})
  result = super(field_name, options)

  if options[:aka]
    Array(options[:aka]).each do |as|
      self._error_map = _error_map.merge(as.to_sym => field_name.to_sym)
    end
  end

  result
end

Public Instance Methods

submit() click to toggle source

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

# File lib/subroutine/op.rb, line 92
def submit
  submit!
rescue Exception => e
  if e.respond_to?(:record)
    inherit_errors(e.record) unless e.record == self
    false
  else
    raise
  end
end
submit!() click to toggle source
# File lib/subroutine/op.rb, line 68
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!
    true
  else
    raise _failure_class, self
  end
end

Protected Instance Methods

inherit_errors(error_object) click to toggle source

applies the errors in error_object to self returns false so failure cases can end with this invocation

# File lib/subroutine/op.rb, line 132
def inherit_errors(error_object)
  error_object = error_object.errors if error_object.respond_to?(:errors)

  error_object.each do |k, v|
    if respond_to?(k)
      errors.add(k, v)
    elsif _error_map[k.to_sym]
      errors.add(_error_map[k.to_sym], v)
    else
      errors.add(:base, error_object.full_message(k, v))
    end
  end

  false
end
observe_perform() { || ... } click to toggle source
# File lib/subroutine/op.rb, line 114
def observe_perform
  yield
end
observe_submission() { || ... } click to toggle source

these enable you to 1) add log output or 2) add performance monitoring such as skylight.

# File lib/subroutine/op.rb, line 106
def observe_submission
  yield
end
observe_validation() { || ... } click to toggle source
# File lib/subroutine/op.rb, line 110
def observe_validation
  yield
end
perform() click to toggle source

implement this in your concrete class.

# File lib/subroutine/op.rb, line 126
def perform
  raise NotImplementedError
end
validate_and_perform() click to toggle source
# File lib/subroutine/op.rb, line 118
def validate_and_perform
  bool = observe_validation { valid? }
  return false unless bool

  observe_perform { perform }
end