synchronizer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1 -0
- data/Rakefile +3 -0
- data/app/models/synchronizer/import_record.rb +34 -0
- data/app/observers/synchronizer/local_records_observer.rb +8 -0
- data/db/migrate/20140311134009_create_synchronizer_import_records.rb +18 -0
- data/lib/.DS_Store +0 -0
- data/lib/synchronizer.rb +13 -0
- data/lib/synchronizer/base.rb +149 -0
- data/lib/synchronizer/engine.rb +5 -0
- data/lib/synchronizer/version.rb +5 -0
- data/synchronizer.gemspec +23 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61222109893157d0e10d2a1ef506e7541fdff9da
|
4
|
+
data.tar.gz: 6e845198bd498c537dd6fbde564c24debde12886
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 74b3231ec753d3f89bf2acd430854de9888ea4a090dffe8084fc9dfd1dc40252e907f900f82a66c454c9601754fe78e727c92e88e80feeadfdd61dd9147b7f39
|
7
|
+
data.tar.gz: 8d277680cb28185eed144cd0c409f4372db725d9b0f881e82b5af5446dc4ebb12a3a367356e0c03bf10f49081439523a30c1a723f68ad85ebc7b678eb4b9b832
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format Fuubar --color --drb
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 vad4msiu
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Synchronizer
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Synchronizer::ImportRecord < ActiveRecord::Base
|
2
|
+
belongs_to(:local_record,
|
3
|
+
:polymorphic => true,
|
4
|
+
:foreign_key => :local_id,
|
5
|
+
:foreign_type => :local_type,
|
6
|
+
:dependent => :destroy
|
7
|
+
)
|
8
|
+
|
9
|
+
Synchronizer.config.local_types.each do |type|
|
10
|
+
scope "only_#{type.underscore}", -> { where(:local_type => type) }
|
11
|
+
end
|
12
|
+
scope :with_type, ->(type) { where(:local_type => type.to_s) }
|
13
|
+
scope :with_external_ids, ->(ids) { where(:external_id => ids) }
|
14
|
+
scope :with_local_ids, ->(ids) { where(:local_id => ids) }
|
15
|
+
|
16
|
+
validates :local_type, :inclusion => { :in => ->(o) { Synchronizer.config.local_types }}
|
17
|
+
validates :local_record, :presence => true
|
18
|
+
|
19
|
+
def self.find_by_external_id!(external_id)
|
20
|
+
find_by!(:external_id => external_id)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.find_by_local_record(local_record)
|
24
|
+
find_by(:local_record => local_record)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.find_by_external_id_and_local_type(external_id, local_type)
|
28
|
+
find_by(:external_id => external_id, :local_type => local_type)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.find_by_local_id_and_local_type(local_id, local_type)
|
32
|
+
find_by(:local_id => local_id, :local_type => local_type)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class Synchronizer::LocalRecordsObserver < ActiveRecord::Observer
|
2
|
+
observe Synchronizer.config.local_types
|
3
|
+
|
4
|
+
def after_destroy(record)
|
5
|
+
record = Synchronizer.config.import_class_name.constantize.find_by_local_record(record)
|
6
|
+
record.destroy! if record
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class CreateSynchronizerImportRecords < ActiveRecord::Migration
|
4
|
+
def change
|
5
|
+
create_table "synchronizer_import_records", :force => true do |t|
|
6
|
+
t.string "local_type", :null => false
|
7
|
+
t.integer "local_id", :null => false
|
8
|
+
t.string "external_id", :null => false
|
9
|
+
t.datetime "created_at", :null => false
|
10
|
+
t.datetime "updated_at", :null => false
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index "synchronizer_import_records", ["external_id"], :name => "index_synchronizer_import_records_on_external_id"
|
14
|
+
add_index "synchronizer_import_records", ["external_id", "local_type"], :name => "index_synchronizer_import_records_on_external_id_and_local_type"
|
15
|
+
add_index "synchronizer_import_records", ["local_id", "local_type"], :name => "index_synchronizer_import_records_on_local_id_and_local_type"
|
16
|
+
add_index "synchronizer_import_records", ["local_type"], :name => "index_synchronizer_import_records_on_local_type"
|
17
|
+
end
|
18
|
+
end
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/synchronizer.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module Synchronizer
|
4
|
+
include ActiveSupport::Configurable
|
5
|
+
|
6
|
+
config.local_types ||= []
|
7
|
+
config.import_class_name ||= 'Synchronizer::ImportRecord'
|
8
|
+
end
|
9
|
+
|
10
|
+
require "synchronizer/engine"
|
11
|
+
require "synchronizer/base"
|
12
|
+
require "synchronizer/version"
|
13
|
+
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class Synchronizer::Base
|
4
|
+
attr_reader :external_attrs
|
5
|
+
|
6
|
+
def initialize(external_attrs)
|
7
|
+
@external_attrs = external_attrs.with_indifferent_access
|
8
|
+
end
|
9
|
+
|
10
|
+
def sync
|
11
|
+
error_handler do
|
12
|
+
if import_record.present?
|
13
|
+
update_local_record!
|
14
|
+
else
|
15
|
+
create_local_record!
|
16
|
+
create_import_record!
|
17
|
+
end
|
18
|
+
|
19
|
+
return true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def local_record
|
24
|
+
@local_record ||= import_record.try(:local_record)
|
25
|
+
end
|
26
|
+
|
27
|
+
def local_attrs
|
28
|
+
@local_attrs ||= mapping
|
29
|
+
end
|
30
|
+
|
31
|
+
def import_record
|
32
|
+
@import_record ||= self.class.import_class.find_by_external_id_and_local_type(external_id, local_type)
|
33
|
+
end
|
34
|
+
|
35
|
+
def external_id
|
36
|
+
external_attrs[Synchronizer.config.external_id || :id]
|
37
|
+
end
|
38
|
+
|
39
|
+
def mapping
|
40
|
+
raise "Owerwrite this method for mapping external_attrs to local!"
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def update_local_record!
|
46
|
+
local_record.update_attributes!(local_attrs)
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_local_record!
|
50
|
+
@local_record = active_record_class.create!(local_attrs)
|
51
|
+
end
|
52
|
+
|
53
|
+
def create_import_record!
|
54
|
+
import_record = import_class.new(
|
55
|
+
:local_id => local_record.id,
|
56
|
+
:external_id => external_id,
|
57
|
+
)
|
58
|
+
import_record.local_type = local_type
|
59
|
+
import_record.save!
|
60
|
+
end
|
61
|
+
|
62
|
+
def notify_error(e)
|
63
|
+
message = "Error #{self.class.name.inspect} #{e.message}\n" \
|
64
|
+
"local_record = #{local_record.inspect}\n" \
|
65
|
+
"local_attrs = #{@local_attrs.inspect}\n" \
|
66
|
+
"external_attrs = #{external_attrs.inspect}\n" \
|
67
|
+
"#{e.backtrace.join("\n")}"
|
68
|
+
|
69
|
+
@@errors << message
|
70
|
+
Rails.logger.error(message)
|
71
|
+
end
|
72
|
+
|
73
|
+
def active_record_class
|
74
|
+
self.class.active_record_class
|
75
|
+
end
|
76
|
+
|
77
|
+
def import_class
|
78
|
+
self.class.import_class
|
79
|
+
end
|
80
|
+
|
81
|
+
def local_type
|
82
|
+
self.class.local_type
|
83
|
+
end
|
84
|
+
|
85
|
+
def error_handler &block
|
86
|
+
ActiveRecord::Base.transaction do
|
87
|
+
yield
|
88
|
+
end
|
89
|
+
rescue Exception => e
|
90
|
+
notify_error(e)
|
91
|
+
return false
|
92
|
+
end
|
93
|
+
|
94
|
+
class << self
|
95
|
+
def sync(external_items, options = {})
|
96
|
+
@@errors = []
|
97
|
+
|
98
|
+
destroy_missed = options[:destroy_missed] == false ? false : true
|
99
|
+
model_sync_name = name.demodulize
|
100
|
+
|
101
|
+
old_import_ids = options[:scope_import_record_ids] || import_records.pluck(:id)
|
102
|
+
current_import_ids = []
|
103
|
+
|
104
|
+
external_items.each do |item_attrs|
|
105
|
+
sync_object = new(item_attrs)
|
106
|
+
if sync_object.sync
|
107
|
+
current_import_ids << sync_object.import_record.id
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
if destroy_missed
|
112
|
+
delete_missing_import_records(old_import_ids, current_import_ids)
|
113
|
+
end
|
114
|
+
|
115
|
+
return @@errors
|
116
|
+
end
|
117
|
+
|
118
|
+
def delete_missing_import_records(old_ids, current_ids)
|
119
|
+
deleted_ids = old_ids - current_ids
|
120
|
+
|
121
|
+
import_class.find(deleted_ids).each do |import_record|
|
122
|
+
unless import_record.destroy
|
123
|
+
import_error_message = import_record.errors.full_messages.join('.')
|
124
|
+
local_error_message = import_record.errors.full_messages.join('.')
|
125
|
+
message = "Error import_error_message = #{error_message} " \
|
126
|
+
"local_error_message = #{local_error_message} " \
|
127
|
+
"when deleting #{import_record}"
|
128
|
+
@@errors << message
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def import_records
|
134
|
+
import_class.with_type(local_type)
|
135
|
+
end
|
136
|
+
|
137
|
+
def import_class
|
138
|
+
Synchronizer.config.import_class_name.constantize
|
139
|
+
end
|
140
|
+
|
141
|
+
def local_type
|
142
|
+
"#{active_record_class.name}"
|
143
|
+
end
|
144
|
+
|
145
|
+
def active_record_class
|
146
|
+
name.demodulize.sub(/Synchronizer$/, '').constantize
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require "synchronizer/version"
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = "synchronizer"
|
10
|
+
spec.version = Synchronizer::VERSION
|
11
|
+
spec.authors = ["vad4msiu"]
|
12
|
+
spec.email = ["vad4msiu@gmail.com"]
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.summary = %q{}
|
15
|
+
spec.homepage = ""
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "rails", "~> 4.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: synchronizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vad4msiu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
description: ''
|
28
|
+
email:
|
29
|
+
- vad4msiu@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".DS_Store"
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- Gemfile
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- app/models/synchronizer/import_record.rb
|
42
|
+
- app/observers/synchronizer/local_records_observer.rb
|
43
|
+
- db/migrate/20140311134009_create_synchronizer_import_records.rb
|
44
|
+
- lib/.DS_Store
|
45
|
+
- lib/synchronizer.rb
|
46
|
+
- lib/synchronizer/base.rb
|
47
|
+
- lib/synchronizer/engine.rb
|
48
|
+
- lib/synchronizer/version.rb
|
49
|
+
- synchronizer.gemspec
|
50
|
+
homepage: ''
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.2.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: ''
|
74
|
+
test_files: []
|