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
- .outputs(*names) ⇒ 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.
163 164 165 166 167 168 |
# File 'lib/subroutine/op.rb', line 163 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.
160 161 162 |
# File 'lib/subroutine/op.rb', line 160 def defaults @defaults end |
#original_params ⇒ Object (readonly)
Returns the value of attribute original_params.
159 160 161 |
# File 'lib/subroutine/op.rb', line 159 def original_params @original_params end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
160 161 162 |
# File 'lib/subroutine/op.rb', line 160 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’
38 39 40 41 42 43 44 |
# File 'lib/subroutine/op.rb', line 38 def field(*fields) = fields. fields.each do |f| _field(f, ) end end |
.ignore_error(*field_names) ⇒ Object Also known as: ignore_errors
60 61 62 63 64 |
# File 'lib/subroutine/op.rb', line 60 def ignore_error(*field_names) field_names.each do |f| _ignore_errors(f) end end |
.inherited(child) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/subroutine/op.rb', line 87 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/subroutine/op.rb', line 67 def inputs_from(*ops) = ops. excepts = .key?(:except) ? Array(.delete(:except)) : nil onlys = .key?(:only) ? Array(.delete(:only)) : nil ops.each do |op| op._fields.each_pair do |field_name, | next if excepts && excepts.include?(field_name) next if onlys && !onlys.include?(field_name) if [:association] include ::Subroutine::Association unless included_modules.include?(::Subroutine::Association) association(field_name, ) else field(field_name, ) end end end end |
.outputs(*names) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/subroutine/op.rb', line 47 def outputs(*names) = names. names.each do |name| self._outputs = _outputs.merge(name.to_sym => DEFAULT_OUTPUT_OPTIONS.merge()) class_eval <<-EV, __FILE__, __LINE__ + 1 def #{name} @outputs[:#{name}] end EV end end |
.submit(*args) ⇒ Object
101 102 103 104 105 |
# File 'lib/subroutine/op.rb', line 101 def submit(*args) op = new(*args) op.submit op end |
.submit!(*args) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/subroutine/op.rb', line 94 def submit!(*args) op = new(*args) op.submit! op end |
Instance Method Details
#errors ⇒ Object
170 171 172 |
# File 'lib/subroutine/op.rb', line 170 def errors @filtered_errors ||= Subroutine::FilteredErrors.new(super) end |
#output(name, value) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/subroutine/op.rb', line 174 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
221 222 223 |
# File 'lib/subroutine/op.rb', line 221 def params_with_defaults @defaults.merge(@params) end |
#submit ⇒ Object
the action which should be invoked upon form submission (from the controller)
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/subroutine/op.rb', line 210 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
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/subroutine/op.rb', line 181 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 raise ::Subroutine::Failure.new(self) else raise end end if errors.empty? _outputs.each_pair do |name, config| if config[:required] && !@outputs.key?(name) raise ::Subroutine::OutputNotSetError.new(name) end end true else raise ::Subroutine::Failure.new(self) end end |