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.



109
110
111
112
# File 'lib/subroutine/op.rb', line 109

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

Instance Attribute Details

#original_paramsObject (readonly)

Returns the value of attribute original_params.



105
106
107
# File 'lib/subroutine/op.rb', line 105

def original_params
  @original_params
end

#paramsObject (readonly)

Returns the value of attribute params.



106
107
108
# File 'lib/subroutine/op.rb', line 106

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’



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

def field(*fields)
  options = fields.extract_options!

  fields.each do |f|
    _field(f, options)
  end
end

.inherited(child) ⇒ Object



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

def inherited(child)
  super
  child._fields = self._fields.dup
end

.inputs_from(*ops) ⇒ Object



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

def inputs_from(*ops)
  ops.each do |op|
    op._fields.each_pair do |field_name, options|
      field(field_name, options)
    end
  end
end

.submit(*args) ⇒ Object



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

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

.submit!(*args) ⇒ Object



58
59
60
61
62
63
# File 'lib/subroutine/op.rb', line 58

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

  op
end

Instance Method Details

#submitObject

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/subroutine/op.rb', line 123

def submit
  observe_submission do
    @params = filter_params(@original_params)

    set_accessors(@params)

    validate_and_perform
  end

rescue Exception => e

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

#submit!Object



115
116
117
118
119
120
# File 'lib/subroutine/op.rb', line 115

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