Class: Subroutine::Op

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

Constant Summary collapse

DEFAULT_OUTPUT_OPTIONS =
{
  required: true,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fields

#field_provided?, #params_with_defaults, #sanitize_defaults, #sanitize_params, #setup_fields

Constructor Details

#initialize(inputs = {}) ⇒ Op

Returns a new instance of Op.



88
89
90
91
# File 'lib/subroutine/op.rb', line 88

def initialize(inputs = {})
  setup_fields(inputs)
  @outputs = {}
end

Class Method Details

.ignore_error(*field_names) ⇒ Object Also known as: ignore_errors



37
38
39
40
41
# File 'lib/subroutine/op.rb', line 37

def ignore_error(*field_names)
  field_names.each do |f|
    _ignore_errors(f)
  end
end

.outputs(*names) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/subroutine/op.rb', line 24

def outputs(*names)
  options = names.extract_options!
  names.each do |name|
    self._outputs = _outputs.merge(name.to_sym => DEFAULT_OUTPUT_OPTIONS.merge(options))

    class_eval <<-EV, __FILE__, __LINE__ + 1
      def #{name}
        @outputs[:#{name}]
      end
    EV
  end
end

.submit(*args) ⇒ Object



51
52
53
54
55
# File 'lib/subroutine/op.rb', line 51

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

.submit!(*args) ⇒ Object



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

def submit!(*args)
  op = new(*args)
  op.submit!

  op
end

Instance Method Details

#errorsObject



93
94
95
# File 'lib/subroutine/op.rb', line 93

def errors
  @filtered_errors ||= Subroutine::FilteredErrors.new(super)
end

#output(name, value) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/subroutine/op.rb', line 97

def output(name, value)
  unless _outputs.key?(name.to_sym)
    raise ::Subroutine::UnknownOutputError, name
  end

  @outputs[name.to_sym] = value
end

#submitObject

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



134
135
136
137
138
139
140
141
142
143
# File 'lib/subroutine/op.rb', line 134

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!Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/subroutine/op.rb', line 105

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 = ::Subroutine::Failure.new(self)
      raise new_e, new_e.message, e.backtrace
    else
      raise
    end
  end

  if errors.empty?
    _outputs.each_pair do |name, config|
      if config[:required] && !@outputs.key?(name)
        raise ::Subroutine::OutputNotSetError, name
      end
    end

    true
  else
    raise ::Subroutine::Failure, self
  end
end