Class: Swim::Change
- Inherits:
-
Object
- Object
- Swim::Change
- Defined in:
- lib/swim/change.rb
Instance Attribute Summary collapse
-
#change_type ⇒ Object
Returns the value of attribute change_type.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#key ⇒ Object
Returns the value of attribute key.
-
#new_value ⇒ Object
Returns the value of attribute new_value.
-
#obj_class ⇒ Object
Returns the value of attribute obj_class.
-
#obj_id ⇒ Object
Returns the value of attribute obj_id.
-
#old_value ⇒ Object
Returns the value of attribute old_value.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #delete(i) ⇒ Object
-
#initialize(args) ⇒ Change
constructor
A new instance of Change.
- #insert ⇒ Object
- #item ⇒ Object
- #new(args) ⇒ Object
- #process ⇒ Object
- #update(i) ⇒ Object
Constructor Details
#initialize(args) ⇒ Change
Returns a new instance of Change.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/swim/change.rb', line 4 def initialize(args) obj_class = args[:obj_class] obj_id = args[:obj_id] change_type = args[:change_type] key = args[:key] old_value = args[:old_value] new_value = args[:new_value] status = args[:status] errors = args[:errors] end |
Instance Attribute Details
#change_type ⇒ Object
Returns the value of attribute change_type.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def change_type @change_type end |
#errors ⇒ Object
Returns the value of attribute errors.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def errors @errors end |
#key ⇒ Object
Returns the value of attribute key.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def key @key end |
#new_value ⇒ Object
Returns the value of attribute new_value.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def new_value @new_value end |
#obj_class ⇒ Object
Returns the value of attribute obj_class.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def obj_class @obj_class end |
#obj_id ⇒ Object
Returns the value of attribute obj_id.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def obj_id @obj_id end |
#old_value ⇒ Object
Returns the value of attribute old_value.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def old_value @old_value end |
#status ⇒ Object
Returns the value of attribute status.
2 3 4 |
# File 'lib/swim/change.rb', line 2 def status @status end |
Instance Method Details
#delete(i) ⇒ Object
26 27 28 |
# File 'lib/swim/change.rb', line 26 def delete(i) i.destroy end |
#insert ⇒ Object
30 31 32 33 |
# File 'lib/swim/change.rb', line 30 def insert i = obj_class.constantize.send(:new, new_value) return i end |
#item ⇒ Object
18 19 20 |
# File 'lib/swim/change.rb', line 18 def item obj_class.constantize.send(:find, obj_id) end |
#new(args) ⇒ Object
15 16 |
# File 'lib/swim/change.rb', line 15 def new(args) end |
#process ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/swim/change.rb', line 35 def process if change_type == :update i = item status = update(i) ? "succeeded" : "failed" elsif change_type == :delete i = item status = delete(i) ? "succeeded" : "failed" elsif change_type == :insert i = insert if insert.save status = "succeeded" else errors = i.errors status = "failed" end end if status == "succeeded" return true else errors = item.errors return false end end |
#update(i) ⇒ Object
22 23 24 |
# File 'lib/swim/change.rb', line 22 def update(i) i.update_attribute(key, new_value) end |