Class: Swim::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/swim/change.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_typeObject

Returns the value of attribute change_type.



2
3
4
# File 'lib/swim/change.rb', line 2

def change_type
  @change_type
end

#errorsObject

Returns the value of attribute errors.



2
3
4
# File 'lib/swim/change.rb', line 2

def errors
  @errors
end

#keyObject

Returns the value of attribute key.



2
3
4
# File 'lib/swim/change.rb', line 2

def key
  @key
end

#new_valueObject

Returns the value of attribute new_value.



2
3
4
# File 'lib/swim/change.rb', line 2

def new_value
  @new_value
end

#obj_classObject

Returns the value of attribute obj_class.



2
3
4
# File 'lib/swim/change.rb', line 2

def obj_class
  @obj_class
end

#obj_idObject

Returns the value of attribute obj_id.



2
3
4
# File 'lib/swim/change.rb', line 2

def obj_id
  @obj_id
end

#old_valueObject

Returns the value of attribute old_value.



2
3
4
# File 'lib/swim/change.rb', line 2

def old_value
  @old_value
end

#statusObject

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

#insertObject



30
31
32
33
# File 'lib/swim/change.rb', line 30

def insert
  i = obj_class.constantize.send(:new, new_value)
  return i
end

#itemObject



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

#processObject



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