active_git 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/.gitignore +19 -18
  2. data/.travis.yml +7 -0
  3. data/Gemfile +4 -4
  4. data/LICENSE +21 -21
  5. data/README.md +31 -29
  6. data/Rakefile +2 -2
  7. data/active_git.gemspec +25 -25
  8. data/lib/active_git.rb +47 -46
  9. data/lib/active_git/active_record_extension.rb +34 -49
  10. data/lib/active_git/commands.rb +146 -146
  11. data/lib/active_git/configuration.rb +28 -28
  12. data/lib/active_git/events/db_create.rb +8 -8
  13. data/lib/active_git/events/db_delete.rb +12 -12
  14. data/lib/active_git/events/db_delete_all.rb +15 -15
  15. data/lib/active_git/events/db_event.rb +28 -27
  16. data/lib/active_git/events/db_update.rb +14 -14
  17. data/lib/active_git/events/file_delete.rb +11 -11
  18. data/lib/active_git/events/file_event.rb +31 -31
  19. data/lib/active_git/events/file_save.rb +13 -13
  20. data/lib/active_git/events/folder_remove.rb +15 -15
  21. data/lib/active_git/inflector.rb +21 -0
  22. data/lib/active_git/synchronization_error.rb +32 -32
  23. data/lib/active_git/synchronizer.rb +53 -53
  24. data/lib/active_git/version.rb +3 -3
  25. data/spec/active_record_extension_spec.rb +60 -50
  26. data/spec/commands_spec.rb +245 -245
  27. data/spec/inflector_spec.rb +37 -0
  28. data/spec/migrations/20121004135939_create_countries.rb +9 -9
  29. data/spec/migrations/20121004135940_create_languages.rb +9 -9
  30. data/spec/migrations/20121030163114_create_brands.rb +9 -9
  31. data/spec/migrations/20130315192821_create_customers.rb +9 -0
  32. data/spec/spec_helper.rb +35 -23
  33. data/spec/support/helpers/file_helper.rb +47 -43
  34. data/spec/support/models/brand.rb +2 -4
  35. data/spec/support/models/country.rb +1 -2
  36. data/spec/support/models/customer.rb +6 -0
  37. data/spec/support/models/language.rb +2 -4
  38. data/spec/synchronization_spec.rb +100 -100
  39. metadata +56 -17
@@ -1,29 +1,29 @@
1
- module ActiveGit
2
- class Configuration
3
-
4
- def working_path
5
- @working_path.is_a?(Proc) ? @working_path.call : @working_path
6
- end
7
-
8
- def working_path=(path)
9
- @working_path = path
10
- end
11
-
12
- def bare_path
13
- @bare_path.is_a?(Proc) ? @bare_path.call : @bare_path
14
- end
15
-
16
- def bare_path=(path)
17
- @bare_path = path
18
- end
19
-
20
- def logger
21
- GitWrapper.logger
22
- end
23
-
24
- def logger=(logger)
25
- GitWrapper.logger = logger
26
- end
27
-
28
- end
1
+ module ActiveGit
2
+ class Configuration
3
+
4
+ def working_path
5
+ @working_path.is_a?(Proc) ? @working_path.call : @working_path
6
+ end
7
+
8
+ def working_path=(path)
9
+ @working_path = path
10
+ end
11
+
12
+ def bare_path
13
+ @bare_path.is_a?(Proc) ? @bare_path.call : @bare_path
14
+ end
15
+
16
+ def bare_path=(path)
17
+ @bare_path = path
18
+ end
19
+
20
+ def logger
21
+ GitWrapper.logger
22
+ end
23
+
24
+ def logger=(logger)
25
+ GitWrapper.logger = logger
26
+ end
27
+
28
+ end
29
29
  end
@@ -1,9 +1,9 @@
1
- module ActiveGit
2
- class DbCreate < DbEvent
3
-
4
- def synchronize(synchronizer)
5
- synchronizer.bulk_insert data
6
- end
7
-
8
- end
1
+ module ActiveGit
2
+ class DbCreate < DbEvent
3
+
4
+ def synchronize(synchronizer)
5
+ synchronizer.bulk_insert data
6
+ end
7
+
8
+ end
9
9
  end
@@ -1,13 +1,13 @@
1
- module ActiveGit
2
- class DbDelete < DbEvent
3
-
4
- def synchronize(synchronizer)
5
- synchronizer.define_job do
6
- ActiveGit.configuration.logger.debug "[ActiveGit] Deleting #{model.model_name} #{model_id}"
7
- record = model.find_by_id(model_id)
8
- record.delete if record
9
- end
10
- end
11
-
12
- end
1
+ module ActiveGit
2
+ class DbDelete < DbEvent
3
+
4
+ def synchronize(synchronizer)
5
+ synchronizer.define_job do
6
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting #{model.model_name} #{model_id}"
7
+ record = model.find_by_id(model_id)
8
+ record.delete if record
9
+ end
10
+ end
11
+
12
+ end
13
13
  end
@@ -1,16 +1,16 @@
1
- module ActiveGit
2
- class DbDeleteAll
3
-
4
- def initialize(model)
5
- @model = model
6
- end
7
-
8
- def synchronize(synchronizer)
9
- synchronizer.define_job do
10
- ActiveGit.configuration.logger.debug "[ActiveGit] Deleting all #{@model.model_name} models"
11
- @model.delete_all
12
- end
13
- end
14
-
15
- end
1
+ module ActiveGit
2
+ class DbDeleteAll
3
+
4
+ def initialize(model)
5
+ @model = model
6
+ end
7
+
8
+ def synchronize(synchronizer)
9
+ synchronizer.define_job do
10
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting all #{@model.model_name} models"
11
+ @model.delete_all
12
+ end
13
+ end
14
+
15
+ end
16
16
  end
@@ -1,28 +1,29 @@
1
- module ActiveGit
2
- class DbEvent
3
-
4
- def initialize(file_name)
5
- @file_name = file_name
6
- end
7
-
8
- def synchronize(synchronizer)
9
- raise 'Must implement in subclass'
10
- end
11
-
12
- protected
13
-
14
- def model
15
- @model ||= File.dirname(@file_name).split(/\/|\\/).pop.classify.constantize
16
- end
17
-
18
- def model_id
19
- File.basename(@file_name, '.json')
20
- end
21
-
22
- def data
23
- json = File.open(@file_name, 'r') { |f| f.readlines.join("\n") }
24
- model.from_json(json)
25
- end
26
-
27
- end
1
+ module ActiveGit
2
+ class DbEvent
3
+
4
+ def initialize(file_name, working_path=nil)
5
+ @file_name = file_name
6
+ @working_path = working_path || ActiveGit.configuration.working_path
7
+ end
8
+
9
+ def synchronize(synchronizer)
10
+ raise 'Must implement in subclass'
11
+ end
12
+
13
+ protected
14
+
15
+ def model
16
+ @model ||= Inflector.model(@file_name, @working_path)
17
+ end
18
+
19
+ def model_id
20
+ Inflector.model_id @file_name
21
+ end
22
+
23
+ def data
24
+ json = File.open(@file_name, 'r') { |f| f.readlines.join("\n") }
25
+ model.from_json(json)
26
+ end
27
+
28
+ end
28
29
  end
@@ -1,15 +1,15 @@
1
- module ActiveGit
2
- class DbUpdate < DbEvent
3
-
4
- def synchronize(synchronizer)
5
- synchronizer.bulk_insert data
6
-
7
- synchronizer.define_job do
8
- ActiveGit.configuration.logger.debug "[ActiveGit] Deleting #{data.class.model_name} #{data.id}"
9
- record = data.class.find_by_id(data.id)
10
- record.delete if record
11
- end
12
- end
13
-
14
- end
1
+ module ActiveGit
2
+ class DbUpdate < DbEvent
3
+
4
+ def synchronize(synchronizer)
5
+ synchronizer.bulk_insert data
6
+
7
+ synchronizer.define_job do
8
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting #{model.model_name} #{model_id}"
9
+ record = model.find_by_id(model_id)
10
+ record.delete if record
11
+ end
12
+ end
13
+
14
+ end
15
15
  end
@@ -1,12 +1,12 @@
1
- module ActiveGit
2
- class FileDelete < FileEvent
3
-
4
- def synchronize(synchronizer)
5
- synchronizer.define_job do
6
- ActiveGit.configuration.logger.debug "[ActiveGit] Deleting file #{file_name}"
7
- File.delete(file_name) if File.exist?(file_name)
8
- end
9
- end
10
-
11
- end
1
+ module ActiveGit
2
+ class FileDelete < FileEvent
3
+
4
+ def synchronize(synchronizer)
5
+ synchronizer.define_job do
6
+ ActiveGit.configuration.logger.debug "[ActiveGit] Deleting file #{file_name}"
7
+ File.delete(file_name) if File.exist?(file_name)
8
+ end
9
+ end
10
+
11
+ end
12
12
  end
@@ -1,32 +1,32 @@
1
- module ActiveGit
2
- class FileEvent
3
-
4
- def initialize(data, path=nil)
5
- @data = data
6
- @path = path || ActiveGit.configuration.working_path
7
- end
8
-
9
- def synchronize(synchronizer)
10
- raise 'Must implement in subclass'
11
- end
12
-
13
- protected
14
-
15
- def model
16
- @data.class.to_s.classify.constantize
17
- end
18
-
19
- def model_path
20
- "#{@path}/#{model.table_name}"
21
- end
22
-
23
- def file_name
24
- "#{model_path}/#{@data.id}.json"
25
- end
26
-
27
- def json
28
- JSON.pretty_generate(@data.attributes)
29
- end
30
-
31
- end
1
+ module ActiveGit
2
+ class FileEvent
3
+
4
+ def initialize(data, working_path=nil)
5
+ @data = data
6
+ @working_path = working_path || ActiveGit.configuration.working_path
7
+ end
8
+
9
+ def synchronize(synchronizer)
10
+ raise 'Must implement in subclass'
11
+ end
12
+
13
+ protected
14
+
15
+ def model
16
+ @data.class
17
+ end
18
+
19
+ def model_path
20
+ Inflector.dirname(model, @working_path)
21
+ end
22
+
23
+ def file_name
24
+ Inflector.filename(@data, @working_path)
25
+ end
26
+
27
+ def json
28
+ JSON.pretty_generate(@data.attributes)
29
+ end
30
+
31
+ end
32
32
  end
@@ -1,14 +1,14 @@
1
- module ActiveGit
2
- class FileSave < FileEvent
3
-
4
- def synchronize(synchronizer)
5
- synchronizer.define_job do
6
- ActiveGit.configuration.logger.debug "[ActiveGit] Writing file #{file_name}"
7
- FileUtils.mkpath(File.dirname(file_name)) unless Dir.exist?(File.dirname(file_name))
8
- File.open(file_name, 'w') { |f| f.puts json }
9
- end
10
-
11
- end
12
-
13
- end
1
+ module ActiveGit
2
+ class FileSave < FileEvent
3
+
4
+ def synchronize(synchronizer)
5
+ synchronizer.define_job do
6
+ ActiveGit.configuration.logger.debug "[ActiveGit] Writing file #{file_name}"
7
+ FileUtils.mkpath(File.dirname(file_name)) unless Dir.exist?(File.dirname(file_name))
8
+ File.open(file_name, 'w') { |f| f.puts json }
9
+ end
10
+
11
+ end
12
+
13
+ end
14
14
  end
@@ -1,16 +1,16 @@
1
- module ActiveGit
2
- class FolderRemove
3
-
4
- def initialize(path)
5
- @path = path
6
- end
7
-
8
- def synchronize(synchronizer)
9
- synchronizer.define_job do
10
- ActiveGit.configuration.logger.debug "[ActiveGit] Removing folder #{@path}"
11
- FileUtils.rm_rf @path
12
- end
13
- end
14
-
15
- end
1
+ module ActiveGit
2
+ class FolderRemove
3
+
4
+ def initialize(working_path)
5
+ @working_path = working_path
6
+ end
7
+
8
+ def synchronize(synchronizer)
9
+ synchronizer.define_job do
10
+ ActiveGit.configuration.logger.debug "[ActiveGit] Removing folder #{@working_path}"
11
+ FileUtils.rm_rf @working_path
12
+ end
13
+ end
14
+
15
+ end
16
16
  end
@@ -0,0 +1,21 @@
1
+ module ActiveGit
2
+ module Inflector
3
+
4
+ def self.dirname(model, working_path=nil)
5
+ "#{working_path || ActiveGit.configuration.working_path}/#{model.model_name.underscore.pluralize}"
6
+ end
7
+
8
+ def self.filename(instance, working_path=nil)
9
+ "#{dirname(instance.class, working_path || ActiveGit.configuration.working_path)}/#{instance.id}.json"
10
+ end
11
+
12
+ def self.model(filename, working_path=nil)
13
+ File.dirname(filename.gsub(working_path || ActiveGit.configuration.working_path, '')).classify.constantize
14
+ end
15
+
16
+ def self.model_id(filename)
17
+ File.basename(filename, '.json')
18
+ end
19
+
20
+ end
21
+ end
@@ -1,33 +1,33 @@
1
- module ActiveGit
2
- class SynchronizationError < StandardError
3
-
4
- def initialize(models)
5
- @models = models
6
- end
7
-
8
- def message
9
- messages = []
10
- @models.each do |model|
11
- model.errors.full_messages.each do |msg|
12
- attributes = {}
13
- model.attributes.each do |name, value|
14
- attributes[model.class.human_attribute_name(name)] = value
15
- end
16
-
17
- attributes = model.attributes.inject({}) do |memo, item|
18
- memo[model.class.human_attribute_name(item[0])] = item[1]
19
- memo
20
- end
21
-
22
- messages << "#{model.class.model_name.human} - #{msg}\n#{attributes}"
23
- end
24
- end
25
- messages.join("\n")
26
- end
27
-
28
- def to_s
29
- "#{self.class.name} (#{message}):\n#{backtrace.join("\n")}"
30
- end
31
-
32
- end
1
+ module ActiveGit
2
+ class SynchronizationError < StandardError
3
+
4
+ def initialize(models)
5
+ @models = models
6
+ end
7
+
8
+ def message
9
+ messages = []
10
+ @models.each do |model|
11
+ model.errors.full_messages.each do |msg|
12
+ attributes = {}
13
+ model.attributes.each do |name, value|
14
+ attributes[model.class.human_attribute_name(name)] = value
15
+ end
16
+
17
+ attributes = model.attributes.inject({}) do |memo, item|
18
+ memo[model.class.human_attribute_name(item[0])] = item[1]
19
+ memo
20
+ end
21
+
22
+ messages << "#{model.class.model_name.human} - #{msg}\n#{attributes}"
23
+ end
24
+ end
25
+ messages.join("\n")
26
+ end
27
+
28
+ def to_s
29
+ "#{self.class.name} (#{message}):\n#{backtrace.join("\n")}"
30
+ end
31
+
32
+ end
33
33
  end