swim 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODc5OTZiM2FmMmFkMThjY2U4MmRmMjM5MTA5NTY0YmM5NTYwNjg1ZA==
5
+ data.tar.gz: !binary |-
6
+ OGU0ODA0MTlmNDliNWJmMWI2OWI4ZWYyNTdlMjEyMDZiNDY1YWQwZg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OGFmM2RlOWM4YmFlZTEwNDQwM2MyNzkwYmI5OTVjY2Q2YmYyOGUxNmU3M2Y2
10
+ Mzc5ZjY2MTQ2NzQ1NmQ1ZGQ0Y2YyZDBmN2NlMGE4ZTkxNGE0ODI0NDY3Yzky
11
+ YWEyYzMyZGQ0OWMxZTY2ZGQ2YmJkYTllNmFjN2IyMDgyNmE2MWM=
12
+ data.tar.gz: !binary |-
13
+ MDdhMGQ1MDZmYTA0ZmYzNTkzNDNjMGRkMDlmZGU4MDZkNjJiN2YyNjViOGEy
14
+ ODczOTk4ODBkNjcwNWIwMGM0YTZjZTFmYzVkYmVkOWRmMDI4NGQ5M2Q3N2Yz
15
+ ODQ1Yzg0YjNiOTRkYTY5YTk2Nzg2Yjg4YWMyZTk1M2Q0OTNjMTI=
data/.gitignore CHANGED
@@ -16,4 +16,5 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .DS_Store
19
- lib/.DS_Store
19
+ lib/.DS_Store
20
+ .rbenv-version
data/README.md CHANGED
@@ -20,19 +20,19 @@ Or install it yourself as:
20
20
 
21
21
  1. Add a settings_file instance method to your ActiveRecord class.
22
22
 
23
- class Artist < ActiveRecord::Base
24
- def settings_file
25
- File.expand_path("/sample_files/artist_1_settings.json")
26
- end
27
- end
28
-
23
+ class Artist < ActiveRecord::Base
24
+ def settings_file
25
+ File.expand_path("/sample_files/artist_1_settings.json")
26
+ end
27
+ end
28
+
29
29
  2. Add a sync_settings class method to your ActiveRecord class.
30
30
 
31
- class Artist < ActiveRecord::Base
32
- def self.sync_settings
33
- { :albums => {} }
34
- end
35
- end
31
+ class Artist < ActiveRecord::Base
32
+ def self.sync_settings
33
+ { :albums => {} }
34
+ end
35
+ end
36
36
 
37
37
  ## Contributing
38
38
 
@@ -1,7 +1,7 @@
1
1
  # Class to store changes made during SyncTools' import_json_file method.
2
2
  class Swim::Change
3
3
  attr_accessor :obj_class, :obj_id, :change_type, :key, :old_value, :new_value, :status, :errors
4
-
4
+
5
5
  def initialize(attributes = nil)
6
6
  self.obj_class = attributes[:obj_class]
7
7
  self.obj_id = attributes[:obj_id]
@@ -12,7 +12,7 @@ class Swim::Change
12
12
  self.status = attributes[:status]
13
13
  self.errors = attributes[:errors]
14
14
  end
15
-
15
+
16
16
  def to_s
17
17
  if status.nil? || status.empty?
18
18
  verb = "would be"
@@ -27,10 +27,10 @@ class Swim::Change
27
27
  "#{obj_class}(#{obj_id}) #{verb} #{change_type}d (#{ change_type == :insert ? new_value.to_hash : old_value.to_hash })."
28
28
  end
29
29
  end
30
-
30
+
31
31
  def new(args)
32
32
  end
33
-
33
+
34
34
  def process
35
35
  if change_type == :update
36
36
  i = item
@@ -54,25 +54,25 @@ class Swim::Change
54
54
  return false
55
55
  end
56
56
  end
57
-
57
+
58
58
  private
59
-
60
- # Convenience method for accessing the changed object in the database
59
+
60
+ # Convenience method for accessing the changed object in the database
61
61
  def item
62
62
  obj_class.constantize.send(:find, obj_id)
63
63
  end
64
-
65
- # Method to execute the attribute update described by the Change
64
+
65
+ # Method to execute the attribute update described by the Change
66
66
  def update(i)
67
67
  i.update_attribute(key, new_value)
68
68
  end
69
-
70
- # Method to execute the object deletion described by the Change
69
+
70
+ # Method to execute the object deletion described by the Change
71
71
  def delete(i)
72
72
  i.destroy
73
73
  end
74
-
75
- # Method to execute the object insertion described by the Change
74
+
75
+ # Method to execute the object insertion described by the Change
76
76
  def insert
77
77
  i = obj_class.constantize.send(:new, new_value)
78
78
  return i
@@ -13,31 +13,31 @@ end
13
13
 
14
14
  # Tools for syncing a tree of ActiveRecord objects using a JSON file.
15
15
  class SyncTools
16
-
17
- # Save a JSON representaton of the object to a file, specified by
16
+
17
+ # Save a JSON representaton of the object to a file, specified by
18
18
  def self.save_settings(obj)
19
- str = obj.to_json(:include => obj.class.sync_settings )
19
+ str = obj.to_json(:include => obj.class.sync_settings )
20
20
  File.open(File.expand_path(obj.settings_path), 'w+') {|f| f.write(str) }
21
21
  end
22
-
23
- # compares a tree of ActiveRecord objects to a previously-saved JSON file
24
- def self.compare_json_file(obj)
22
+
23
+ # compares a tree of ActiveRecord objects to a previously-saved JSON file
24
+ def self.compare_json_file(obj)
25
25
  unless obj.methods.include?(:settings_path) || obj.methods.include?('settings_path')
26
26
  raise SettingsPathMissing, "#{obj.class.to_s} must define a class method named settings_file."
27
27
  end
28
28
  json = json_file(File.expand_path(obj.settings_path))
29
-
29
+
30
30
  unless obj.class.methods.include?(:sync_settings) || obj.class.methods.include?("sync_settings")
31
31
  raise SyncSettingsMissing, "#{obj.class.to_s} must define a class method named sync_settings that returns a hash."
32
32
  end
33
-
33
+
34
34
  if json[obj.class.to_s.underscore].class.to_s == "Hash"
35
35
  return compare(obj, json[obj.class.to_s.underscore], obj.class.sync_settings)
36
36
  else
37
37
  return compare(obj, json, obj.class.sync_settings)
38
38
  end
39
39
  end
40
-
40
+
41
41
  def self.import_json_file(obj, settings_path)
42
42
  changes = Swim::SyncTools.compare_json_file(self, settings_path)
43
43
  completed = []
@@ -52,13 +52,13 @@ class SyncTools
52
52
  end
53
53
  return { :status => (not_completed.length > 0 ? "incomplete" : "complete"), :changed => completed, :not_completed => not_completed }
54
54
  end
55
-
56
- def self.compare(obj, hsh, settings)
55
+
56
+ def self.compare(obj, hsh, settings)
57
57
  @changes = []
58
58
  if settings.present? && settings[:include]
59
59
  settings[:include].keys.each do |sk|
60
60
  compare_array(obj.send(sk), hsh[sk.to_s], settings[sk], sk)
61
- end
61
+ end
62
62
  elsif settings.present? && settings.keys
63
63
  settings.keys.each do |sk|
64
64
  compare_array(obj.send(sk), hsh[sk.to_s], settings[sk.to_sym], sk)
@@ -67,16 +67,18 @@ class SyncTools
67
67
 
68
68
  obj.attributes.each_pair do |key, value|
69
69
  hsh_value = hsh[key]
70
- if value.class == Time && hsh_value.class == String
70
+
71
+ if value.is_a?(Time) && hsh_value.class == String
71
72
  hsh_value = Time.parse(hsh_value).utc.to_s
72
73
  value = value.utc.to_s
73
- elsif hsh_value.class == Time && value.class == String
74
+ elsif hsh_value.is_a?(Time) && value.class == String
74
75
  hsh_value = hsh_value.utc.to_s
75
76
  value = Time.parse(value).utc.to_s
76
- elsif hsh_value.class == Time && value.class == Time
77
+ elsif hsh_value.is_a?(Time) && value.is_a?(Time)
77
78
  hsh_value = hsh_value.utc.to_s
78
79
  value = value.utc.to_s
79
80
  end
81
+
80
82
  unless value == hsh_value
81
83
  # p "#{obj.class.to_s} #{obj.id} #{key} #{value} != #{hsh_value}"
82
84
  @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)
@@ -86,7 +88,7 @@ class SyncTools
86
88
  end
87
89
  return @changes
88
90
  end
89
-
91
+
90
92
  def self.compare_array(arr, hsh, settings, obj_classname)
91
93
  if arr.length == 0 && hsh.nil?
92
94
  return
@@ -109,8 +111,8 @@ class SyncTools
109
111
  end
110
112
  end
111
113
  end
112
-
113
- # loads and decodes a specified json_file
114
+
115
+ # loads and decodes a specified json_file
114
116
  def self.json_file(settings_path)
115
117
  file = File.open(settings_path, "rb")
116
118
  contents = file.read
@@ -1,3 +1,3 @@
1
1
  module Swim
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'swim'
3
3
 
4
4
  describe Swim do
5
- it 'should return correct version string' do
6
- Swim.version_string.should == "Swim version #{Swim::VERSION}"
7
- end
8
- end
5
+ it 'should return correct version string' do
6
+ Swim.version_string.should == "Swim version #{Swim::VERSION}"
7
+ end
8
+ end
@@ -4,22 +4,22 @@ require 'swim/change'
4
4
  require 'swim/sync_tools'
5
5
 
6
6
  describe SyncTools do
7
-
7
+
8
8
  context "necessary settings" do
9
9
  it "should require the object to define settings_path" do
10
10
  expect { SyncTools::compare_json_file(InvalidClass.new) }.to raise_error(
11
11
  SettingsPathMissing
12
12
  )
13
13
  end
14
-
14
+
15
15
  it "should require the object to define sync_settings" do
16
16
  expect { SyncTools::compare_json_file(AlmostValidClass.new) }.to raise_error(
17
17
  SyncSettingsMissing
18
18
  )
19
19
  end
20
20
  end
21
-
22
- context "compare_json_file" do
21
+
22
+ context "compare_json_file" do
23
23
  it "should return a change when one exists" do
24
24
  artist = Artist.find(1)
25
25
  changes = SyncTools::compare_json_file(artist)
@@ -27,7 +27,7 @@ describe SyncTools do
27
27
  changes.length.should == 1
28
28
  changes.first.should be_an_instance_of(Swim::Change)
29
29
  end
30
-
30
+
31
31
  it "should correctly identify changes" do
32
32
  artist = Artist.find(2)
33
33
  SyncTools.save_settings(artist)
@@ -36,15 +36,15 @@ describe SyncTools do
36
36
  changes.should be_an_instance_of(Array)
37
37
  changes.length.should == 1
38
38
  changes.first.should be_an_instance_of(Swim::Change)
39
- end
39
+ end
40
40
  end
41
-
41
+
42
42
  context "#save_settings" do
43
43
  it "should save settings in a json file" do
44
44
  artist = Artist.find(2)
45
45
  expect { SyncTools.save_settings(artist) }
46
46
  end
47
-
47
+
48
48
  it "settings and object should be identical" do
49
49
  artist = Artist.find(2)
50
50
  SyncTools.save_settings(artist)
@@ -53,5 +53,5 @@ describe SyncTools do
53
53
  changes.length.should == 0
54
54
  end
55
55
  end
56
-
56
+
57
57
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jerry Richardson
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-08 00:00:00.000000000 Z
11
+ date: 2013-06-18 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: sqlite3
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -74,33 +69,26 @@ files:
74
69
  - swim.gemspec
75
70
  homepage: http://disruptive.github.com/swim
76
71
  licenses: []
72
+ metadata: {}
77
73
  post_install_message:
78
74
  rdoc_options: []
79
75
  require_paths:
80
76
  - lib
81
77
  required_ruby_version: !ruby/object:Gem::Requirement
82
- none: false
83
78
  requirements:
84
79
  - - ! '>='
85
80
  - !ruby/object:Gem::Version
86
81
  version: '0'
87
- segments:
88
- - 0
89
- hash: 3293738542906190303
90
82
  required_rubygems_version: !ruby/object:Gem::Requirement
91
- none: false
92
83
  requirements:
93
84
  - - ! '>='
94
85
  - !ruby/object:Gem::Version
95
86
  version: '0'
96
- segments:
97
- - 0
98
- hash: 3293738542906190303
99
87
  requirements: []
100
88
  rubyforge_project:
101
- rubygems_version: 1.8.24
89
+ rubygems_version: 2.0.2
102
90
  signing_key:
103
- specification_version: 3
91
+ specification_version: 4
104
92
  summary: Synchronize ActiveRecord Objects Across Environments with JSON and git
105
93
  test_files:
106
94
  - spec/db/development.sqlite3