Class: SyncTools

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

Class Method Summary collapse

Class Method Details

.compare(obj, hsh, settings) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/swim/sync_tools.rb', line 50

def self.compare(obj, hsh, settings) 
  @changes = []
  if settings.present? && settings[:include]
    settings[:include].keys.each do |sk|
      compare_array(obj.send(sk), hsh[sk.to_s], settings[sk], sk)
    end      
  elsif settings.present? && settings.keys
    settings.keys.each do |sk|
      compare_array(obj.send(sk), hsh[sk.to_s], settings[sk.to_sym], sk)
    end
  end

  obj.attributes.each_pair do |key, value|
    hsh_value = hsh[key]
    if value.class == Time && hsh_value.class == String
      hsh_value = Time.parse(hsh_value).utc.to_s
      value = value.utc.to_s
    elsif hsh_value.class == Time && value.class == String
      hsh_value = hsh_value.utc.to_s
      value = Time.parse(value).utc.to_s
    elsif hsh_value.class == Time && value.class == Time
      hsh_value = hsh_value.utc.to_s
      value = value.utc.to_s
    end
    unless value == hsh_value
      @changes << Swim::Change.new(:obj_class => obj.class.to_s, :obj_id => obj.id, :change_type => :update, :key => key, :old_value => value, :new_value => hsh_value)
    else
      # p "#{obj.class.to_s} #{key} #{value} == #{hsh_value}"
    end
  end
  return @changes
end

.compare_array(arr, hsh, settings, obj_classname) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/swim/sync_tools.rb', line 83

def self.compare_array(arr, hsh, settings, obj_classname)
  if arr.length == 0 && hsh.nil?
    return
  elsif hsh.nil?
    throw "Array (#{arr.collect{|ar| ar.class.to_s }}) has no hash for comparison"
  end
  hsh_members = hsh.collect{ |h| h["id"] }
  arr.each do |a|
    x = hsh.select{ |h| h if h["id"] == a.id }[0]
    hsh_members.delete(x["id"])
    if x.nil? || x.empty?
      @changes << Change.new(:obj_class => a.class.to_s.singularize.capitalize, :obj_id => a.id, :change_type => :delete)
    else
      compare(a, x, settings)
    end
  end
  if hsh_members.length > 0
    hsh_members.each do |hm|
      @changes << Change.new(:obj_class => obj_classname.to_s.singularize.capitalize, :obj_id => hm, :change_type => :insert, :new_value => hsh.select{ |h| h if h["id"] == hm }[0].to_json)
    end
  end
end

.compare_json_file(obj) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/swim/sync_tools.rb', line 18

def self.compare_json_file(obj)    
  unless obj.methods.include?(:settings_file) || obj.methods.include?('settings_file')
    raise SettingsFileMissing, "#{obj.class.to_s} must define a class method named settings_file."
  end
  json = json_file(obj.settings_file)
  
  unless obj.class.methods.include?(:sync_settings) || obj.class.methods.include?("sync_settings")
    raise SyncSettingsMissing, "#{obj.class.to_s} must define a class method named sync_settings that returns a hash."
  end
  
  if json[obj.class.to_s.underscore].class.to_s == "Hash"
    return compare(obj, json[obj.class.to_s.underscore], obj.class.sync_settings)
  else
    return compare(obj, json, obj.class.sync_settings)
  end
end

.import_json_file(obj, settings_filename) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/swim/sync_tools.rb', line 35

def self.import_json_file(obj, settings_filename)
  changes = Swim::SyncTools.compare_json_file(self, settings_file)
  completed = []
  not_completed = []
  changes.each do |ch|
    oc = ch.process
    if oc
      completed << ch
    else
      not_completed << ch
    end
  end
  return { :status => (not_completed.length > 0 ? "incomplete" : "complete"), :changed => completed, :not_completed => not_completed }
end

.json_file(settings_filename) ⇒ Object



12
13
14
15
16
# File 'lib/swim/sync_tools.rb', line 12

def self.json_file(settings_filename)
  file = File.open(settings_filename, "rb")
  contents = file.read
  ActiveSupport::JSON.decode(contents)
end