active_git 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +19 -18
- data/.travis.yml +7 -0
- data/Gemfile +4 -4
- data/LICENSE +21 -21
- data/README.md +31 -29
- data/Rakefile +2 -2
- data/active_git.gemspec +25 -25
- data/lib/active_git.rb +47 -46
- data/lib/active_git/active_record_extension.rb +34 -49
- data/lib/active_git/commands.rb +146 -146
- data/lib/active_git/configuration.rb +28 -28
- data/lib/active_git/events/db_create.rb +8 -8
- data/lib/active_git/events/db_delete.rb +12 -12
- data/lib/active_git/events/db_delete_all.rb +15 -15
- data/lib/active_git/events/db_event.rb +28 -27
- data/lib/active_git/events/db_update.rb +14 -14
- data/lib/active_git/events/file_delete.rb +11 -11
- data/lib/active_git/events/file_event.rb +31 -31
- data/lib/active_git/events/file_save.rb +13 -13
- data/lib/active_git/events/folder_remove.rb +15 -15
- data/lib/active_git/inflector.rb +21 -0
- data/lib/active_git/synchronization_error.rb +32 -32
- data/lib/active_git/synchronizer.rb +53 -53
- data/lib/active_git/version.rb +3 -3
- data/spec/active_record_extension_spec.rb +60 -50
- data/spec/commands_spec.rb +245 -245
- data/spec/inflector_spec.rb +37 -0
- data/spec/migrations/20121004135939_create_countries.rb +9 -9
- data/spec/migrations/20121004135940_create_languages.rb +9 -9
- data/spec/migrations/20121030163114_create_brands.rb +9 -9
- data/spec/migrations/20130315192821_create_customers.rb +9 -0
- data/spec/spec_helper.rb +35 -23
- data/spec/support/helpers/file_helper.rb +47 -43
- data/spec/support/models/brand.rb +2 -4
- data/spec/support/models/country.rb +1 -2
- data/spec/support/models/customer.rb +6 -0
- data/spec/support/models/language.rb +2 -4
- data/spec/synchronization_spec.rb +100 -100
- 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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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 #{
|
9
|
-
record =
|
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,
|
5
|
-
@data = data
|
6
|
-
@
|
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
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def file_name
|
24
|
-
|
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(
|
5
|
-
@
|
6
|
-
end
|
7
|
-
|
8
|
-
def synchronize(synchronizer)
|
9
|
-
synchronizer.define_job do
|
10
|
-
ActiveGit.configuration.logger.debug "[ActiveGit] Removing folder #{@
|
11
|
-
FileUtils.rm_rf @
|
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
|