Class: Subroutine::Op
- Inherits:
-
Object
- Object
- Subroutine::Op
- Includes:
- ActiveModel::Model, ActiveModel::Validations::Callbacks
- Defined in:
- lib/subroutine/op.rb
Constant Summary collapse
- DEFAULT_OUTPUT_OPTIONS =
{ required: true }.freeze
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#original_params ⇒ Object
readonly
Returns the value of attribute original_params.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
-
.field(*fields) ⇒ Object
(also: fields)
fields can be provided in the following way: field :field1, :field2 field :field3, :field4, default: ‘my default’.
- .ignore_error(*field_names) ⇒ Object (also: ignore_errors)
- .inherited(child) ⇒ Object
- .inputs_from(*ops) ⇒ Object
- .output(name, options = {}) ⇒ Object
- .submit(*args) ⇒ Object
- .submit!(*args) ⇒ Object
Instance Method Summary collapse
- #errors ⇒ Object
-
#initialize(inputs = {}) ⇒ Op
constructor
A new instance of Op.
- #output(name, value) ⇒ Object
- #params_with_defaults ⇒ Object
-
#submit ⇒ Object
the action which should be invoked upon form submission (from the controller).
- #submit! ⇒ Object
Constructor Details
#initialize(inputs = {}) ⇒ Op
Returns a new instance of Op.
161 162 163 164 165 166 |
# File 'lib/subroutine/op.rb', line 161 def initialize(inputs = {}) @original_params = inputs.with_indifferent_access @params = sanitize_params(@original_params) @defaults = sanitize_defaults @outputs = {} end |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
158 159 160 |
# File 'lib/subroutine/op.rb', line 158 def defaults @defaults end |
#original_params ⇒ Object (readonly)
Returns the value of attribute original_params.
157 158 159 |
# File 'lib/subroutine/op.rb', line 157 def original_params @original_params end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
158 159 160 |
# File 'lib/subroutine/op.rb', line 158 def params @params end |
Class Method Details
.field(*fields) ⇒ Object Also known as: fields
fields can be provided in the following way: field :field1, :field2 field :field3, :field4, default: ‘my default’
40 41 42 43 44 45 46 |
# File 'lib/subroutine/op.rb', line 40 def field(*fields) = fields. fields.each do |f| _field(f, ) end end |
.ignore_error(*field_names) ⇒ Object Also known as: ignore_errors
62 63 64 65 66 |
# File 'lib/subroutine/op.rb', line 62 def ignore_error(*field_names) field_names.each do |f| _ignore_errors(f) end end |
.inherited(child) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/subroutine/op.rb', line 82 def inherited(child) super child._fields = self._fields.dup child._error_map = self._error_map.dup child._error_ignores = self._error_ignores.dup end |
.inputs_from(*ops) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/subroutine/op.rb', line 69 def inputs_from(*ops) ops.each do |op| op._fields.each_pair do |field_name, | if [:association] include ::Subroutine::Association unless included_modules.include?(::Subroutine::Association) association(field_name, ) else field(field_name, ) end end end end |
.output(name, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/subroutine/op.rb', line 50 def output(name, = {}) self._outputs = self._outputs.merge({ name.to_sym => DEFAULT_OUTPUT_OPTIONS.merge() }) class_eval " def \#{name}\n @outputs[:\#{name}]\n end\n EV\nend\n", __FILE__, __LINE__ + 1 |
.submit(*args) ⇒ Object
97 98 99 100 101 |
# File 'lib/subroutine/op.rb', line 97 def submit(*args) op = new(*args) op.submit op end |
.submit!(*args) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/subroutine/op.rb', line 90 def submit!(*args) op = new(*args) op.submit! op end |
Instance Method Details
#errors ⇒ Object
168 169 170 |
# File 'lib/subroutine/op.rb', line 168 def errors @filtered_errors ||= Subroutine::FilteredErrors.new(super) end |
#output(name, value) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/subroutine/op.rb', line 172 def output(name, value) unless _outputs.key?(name.to_sym) raise ::Subroutine::UnknownOutputError.new(name) end @outputs[name.to_sym] = value end |
#params_with_defaults ⇒ Object
212 213 214 |
# File 'lib/subroutine/op.rb', line 212 def params_with_defaults @defaults.merge(@params) end |
#submit ⇒ Object
the action which should be invoked upon form submission (from the controller)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/subroutine/op.rb', line 187 def submit result = observe_submission do validate_and_perform end if result _outputs.each_pair do |name, config| if config[:required] && !@outputs.key?(name) raise ::Subroutine::OutputNotSetError.new(name) end end end result rescue Exception => e if e.respond_to?(:record) inherit_errors(e.record) unless e.record == self false else raise e end end |
#submit! ⇒ Object
179 180 181 182 183 184 |
# File 'lib/subroutine/op.rb', line 179 def submit! unless submit raise ::Subroutine::Failure.new(self) end true end |