Class: Subroutine::Op

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#defaultsObject (readonly)

Returns the value of attribute defaults.



160
161
162
# File 'lib/subroutine/op.rb', line 160

def defaults
  @defaults
end

#original_paramsObject (readonly)

Returns the value of attribute original_params.



159
160
161
# File 'lib/subroutine/op.rb', line 159

def original_params
  @original_params
end

#paramsObject (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)
  options = fields.extract_options!

  fields.each do |f|
    _field(f, options)
  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)
  options = ops.extract_options!
  excepts = options.key?(:except) ? Array(options.delete(:except)) : nil
  onlys = options.key?(:only) ? Array(options.delete(:only)) : nil

  ops.each do |op|
    op._fields.each_pair do |field_name, op_options|
      next if excepts && excepts.include?(field_name)
      next if onlys && !onlys.include?(field_name)

      if op_options[:association]
        include ::Subroutine::Association unless included_modules.include?(::Subroutine::Association)
        association(field_name, op_options)
      else
        field(field_name, op_options)
      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)
  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



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

#errorsObject



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_defaultsObject



221
222
223
# File 'lib/subroutine/op.rb', line 221

def params_with_defaults
  @defaults.merge(@params)
end

#submitObject

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