Class: Subroutine::Op

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs = {}) ⇒ Op

Returns a new instance of Op.



140
141
142
143
144
# File 'lib/subroutine/op.rb', line 140

def initialize(inputs = {})
  @original_params  = inputs.with_indifferent_access
  @params = sanitize_params(@original_params)
  @defaults = sanitize_defaults
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



137
138
139
# File 'lib/subroutine/op.rb', line 137

def defaults
  @defaults
end

#original_paramsObject (readonly)

Returns the value of attribute original_params.



136
137
138
# File 'lib/subroutine/op.rb', line 136

def original_params
  @original_params
end

#paramsObject (readonly)

Returns the value of attribute params.



137
138
139
# File 'lib/subroutine/op.rb', line 137

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’



34
35
36
37
38
39
40
# File 'lib/subroutine/op.rb', line 34

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



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

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

.inherited(child) ⇒ Object



64
65
66
67
68
69
# File 'lib/subroutine/op.rb', line 64

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



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/subroutine/op.rb', line 51

def inputs_from(*ops)
  ops.each do |op|
    op._fields.each_pair do |field_name, options|
      if options[:association]
        include ::Subroutine::Association unless included_modules.include?(::Subroutine::Association)
        association(field_name, options)
      else
        field(field_name, options)
      end
    end
  end
end

.submit(*args) ⇒ Object



79
80
81
82
83
# File 'lib/subroutine/op.rb', line 79

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

.submit!(*args) ⇒ Object



72
73
74
75
76
77
# File 'lib/subroutine/op.rb', line 72

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

  op
end

Instance Method Details

#errorsObject



146
147
148
# File 'lib/subroutine/op.rb', line 146

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

#params_with_defaultsObject



173
174
175
# File 'lib/subroutine/op.rb', line 173

def params_with_defaults
  @defaults.merge(@params)
end

#submitObject

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/subroutine/op.rb', line 158

def submit
  observe_submission do
    validate_and_perform
  end

rescue Exception => e

  if e.respond_to?(:record)
    inherit_errors(e.record) unless e.record == self
    false
  else
    raise e
  end
end

#submit!Object



150
151
152
153
154
155
# File 'lib/subroutine/op.rb', line 150

def submit!
  unless submit
    raise ::Subroutine::Failure.new(self)
  end
  true
end