Class: Swim::Change

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

Overview

Class to store changes made during SyncTools’ import_json_file method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Change

Returns a new instance of Change.



5
6
7
8
9
10
11
12
13
14
# File 'lib/swim/change.rb', line 5

def initialize(attributes = nil)
  self.obj_class   = attributes[:obj_class]
  self.obj_id      = attributes[:obj_id]
  self.change_type = attributes[:change_type]
  self.key         = attributes[:key]
  self.old_value   = attributes[:old_value]
  self.new_value   = attributes[:new_value]
  self.status      = attributes[:status]
  self.errors      = attributes[:errors]
end

Instance Attribute Details

#change_typeObject

Returns the value of attribute change_type.



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

def change_type
  @change_type
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#new_valueObject

Returns the value of attribute new_value.



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

def new_value
  @new_value
end

#obj_classObject

Returns the value of attribute obj_class.



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

def obj_class
  @obj_class
end

#obj_idObject

Returns the value of attribute obj_id.



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

def obj_id
  @obj_id
end

#old_valueObject

Returns the value of attribute old_value.



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

def old_value
  @old_value
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#new(args) ⇒ Object



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

def new(args)
end

#processObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/swim/change.rb', line 34

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

#to_sObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/swim/change.rb', line 16

def to_s
  if status.nil? || status.empty?
    verb = "would be"
  elsif status == "succeeded"
    verb = "was"
  elsif status == "failed"
    verb = "could not be"
  end
  if change_type == :update
    "#{obj_class}(#{obj_id})##{key} #{verb} #{old_value} (instead of #{new_value})."
  else
    "#{obj_class}(#{obj_id}) #{verb} #{change_type}d (#{ change_type == :insert ? new_value.to_hash : old_value.to_hash })."
  end
end