HasRemote 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/HasRemote.gemspec +2 -2
- data/README.rdoc +4 -0
- data/VERSION +1 -1
- data/lib/has_remote/synchronizable.rb +32 -21
- data/lib/has_remote/tasks.rb +3 -3
- metadata +2 -2
data/HasRemote.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{HasRemote}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sjoerd Andringa"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-25}
|
13
13
|
s.description = %q{Bind a remote ActiveResource object to your local ActiveRecord objects, delegate attributes and optionally cache remote attributes locally.}
|
14
14
|
s.email = %q{sjoerd.andringa@innovationfactory.eu}
|
15
15
|
s.extra_rdoc_files = [
|
data/README.rdoc
CHANGED
@@ -120,6 +120,10 @@ certain models by using the <tt>MODELS</tt> variable:
|
|
120
120
|
|
121
121
|
rake hr:sync MODELS=Contact,Company
|
122
122
|
|
123
|
+
To specify additional parameters to send with the request that fetches updated resources use the PARAMS variable:
|
124
|
+
|
125
|
+
rake hr:sync PARAMS="since=01-01-2010&limit=25"
|
126
|
+
|
123
127
|
=== Documentation
|
124
128
|
|
125
129
|
To generate RDocs for this plugin, from the has_remote directory run:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module HasRemote
|
2
|
-
|
2
|
+
|
3
3
|
# Contains class methods regarding synchronization of changed, added and deleted remotes.
|
4
4
|
#
|
5
5
|
# === Synchronization examples
|
@@ -20,8 +20,8 @@ module HasRemote
|
|
20
20
|
# your cache is up to date or use the 'hr:sync' rake task to synchronize all models
|
21
21
|
# from the command line.
|
22
22
|
#
|
23
|
-
# *Note*
|
24
|
-
# All remote resources need to have an 'updated_at' field in order for synchronization to work. Records will
|
23
|
+
# *Note*
|
24
|
+
# All remote resources need to have an 'updated_at' field in order for synchronization to work. Records will
|
25
25
|
# be destroyed if their remote resource's 'deleted_at' time lies before the time of synchronization.
|
26
26
|
#
|
27
27
|
module Synchronizable
|
@@ -31,7 +31,7 @@ module HasRemote
|
|
31
31
|
def cached_attributes
|
32
32
|
@cached_attributes ||= []
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
# Returns all remote objects that have been changed since the given time or one week ago if no
|
36
36
|
# time is given. This may include new and optionally deleted (tagged by a 'deleted_at' attribute) resources.
|
37
37
|
#
|
@@ -47,19 +47,30 @@ module HasRemote
|
|
47
47
|
# User::Remote.find :all, :from => :search, :params => {:updated_since => time.strftime('...') }
|
48
48
|
# end
|
49
49
|
#
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
# *Arguments*
|
51
|
+
#
|
52
|
+
# [<tt>time</tt>:] The given time that defines since (see above)
|
53
|
+
# [<tt>params</tt>:] Optional hash of additional parameters to send with the request (e.g. {:limit => 5})
|
54
|
+
#
|
55
|
+
def changed_remotes_since(time = nil, params = {})
|
56
|
+
time ||= 1.week.ago
|
57
|
+
remote_class.find :all, :from => :updated, :params => {:since => time.to_s}.merge(params)
|
53
58
|
end
|
54
|
-
|
59
|
+
|
55
60
|
# Will update all records that have been created, updated or deleted on the remote host
|
56
61
|
# since the last successful synchronization.
|
57
62
|
#
|
63
|
+
# *Options*
|
64
|
+
#
|
65
|
+
# [<tt>since</tt>:] Optionally override the time as of which updated resources are fetched. Also see <tt>changes_remotes_since</tt>.
|
66
|
+
#
|
67
|
+
# All other options are passed in as parameters to <tt>changes_remotes_since</tt>.
|
68
|
+
#
|
58
69
|
def synchronize!(options = {})
|
59
70
|
logger.info( "*** Start synchronizing #{table_name} at #{Time.now.to_s :long} ***\n" )
|
60
71
|
@sync_count = 0
|
61
72
|
begin
|
62
|
-
changed_objects = changed_remotes_since( options
|
73
|
+
changed_objects = changed_remotes_since( options.delete(:since) || synchronized_at, options )
|
63
74
|
if changed_objects.any?
|
64
75
|
# Do everything within transaction to prevent ending up in half-synchronized situation if an exception is raised.
|
65
76
|
transaction { sync_all_records_for(changed_objects) }
|
@@ -69,19 +80,19 @@ module HasRemote
|
|
69
80
|
rescue => e
|
70
81
|
logger.warn( " - Synchronization of #{table_name} failed: #{e} \n #{e.backtrace}" )
|
71
82
|
else
|
72
|
-
self.synchronized_at = changed_objects.map { |o| time_of_update(o) }.sort.last if changed_objects.any?
|
83
|
+
self.synchronized_at = changed_objects.map { |o| time_of_update(o) }.sort.last if changed_objects.any?
|
73
84
|
logger.info( " - Synchronized #{@sync_count} #{table_name}.\n" ) if @sync_count > 0
|
74
85
|
ensure
|
75
86
|
logger.info( "*** Stopped synchronizing #{table_name} at #{Time.now.to_s :long} ***\n" )
|
76
87
|
end
|
77
88
|
end
|
78
|
-
|
89
|
+
|
79
90
|
# Time of the last successful synchronization.
|
80
91
|
#
|
81
92
|
def synchronized_at
|
82
93
|
HasRemote::Synchronization.for(self.name).latest_change
|
83
94
|
end
|
84
|
-
|
95
|
+
|
85
96
|
private
|
86
97
|
|
87
98
|
def synchronized_at=(time) #:nodoc:
|
@@ -91,7 +102,7 @@ module HasRemote
|
|
91
102
|
def sync_all_records_for(resources) #:nodoc:
|
92
103
|
resources.each { |resource| sync_all_records_for_resource(resource) }
|
93
104
|
end
|
94
|
-
|
105
|
+
|
95
106
|
def sync_all_records_for_resource(resource) #:nodoc:
|
96
107
|
records = find(:all, :conditions => ["#{remote_foreign_key} = ?", resource.send(remote_primary_key)])
|
97
108
|
if records.empty?
|
@@ -100,7 +111,7 @@ module HasRemote
|
|
100
111
|
records.each { |record| sync_record_for_resource(record, resource) }
|
101
112
|
end
|
102
113
|
end
|
103
|
-
|
114
|
+
|
104
115
|
def sync_record_for_resource(record, resource) #:nodoc:
|
105
116
|
if deleted?(resource)
|
106
117
|
delete_record_for_resource(record, resource)
|
@@ -108,7 +119,7 @@ module HasRemote
|
|
108
119
|
update_and_save_record_for_resource(record, resource)
|
109
120
|
end
|
110
121
|
end
|
111
|
-
|
122
|
+
|
112
123
|
def update_and_save_record_for_resource(record, resource) #:nodoc:
|
113
124
|
was_it_new = record.new_record?
|
114
125
|
cached_attributes.each do |remote_attr|
|
@@ -121,24 +132,24 @@ module HasRemote
|
|
121
132
|
logger.info( was_it_new ? " - Created #{name.downcase} with id #{record.id}.\n" : " - Updated #{name.downcase} with id #{record.id}.\n" )
|
122
133
|
end
|
123
134
|
end
|
124
|
-
|
135
|
+
|
125
136
|
def delete_record_for_resource(record, resource) #:nodoc:
|
126
137
|
record.destroy
|
127
138
|
@sync_count += 1
|
128
139
|
logger.info( " - Deleted #{name.downcase} with id #{record.id}.\n" )
|
129
140
|
end
|
130
|
-
|
141
|
+
|
131
142
|
def create_record_for_resource(resource) #:nodoc:
|
132
143
|
update_and_save_record_for_resource(new(remote_foreign_key => resource.send(remote_primary_key)), resource)
|
133
144
|
end
|
134
|
-
|
145
|
+
|
135
146
|
def time_of_update(resource)
|
136
147
|
(resource.respond_to?(:deleted_at) && resource.deleted_at) ? resource.deleted_at : resource.updated_at
|
137
148
|
end
|
138
|
-
|
149
|
+
|
139
150
|
def deleted?(resource)
|
140
151
|
resource.respond_to?(:deleted_at) && resource.deleted_at && resource.deleted_at <= Time.now
|
141
152
|
end
|
142
153
|
|
143
|
-
end
|
144
|
-
end
|
154
|
+
end
|
155
|
+
end
|
data/lib/has_remote/tasks.rb
CHANGED
@@ -3,11 +3,11 @@ namespace :hr do
|
|
3
3
|
desc 'Synchronizes all attributes locally cached by has_remote'
|
4
4
|
task :sync => :environment do
|
5
5
|
models = ENV['MODELS'].nil? ? HasRemote.models : extract_models
|
6
|
-
|
6
|
+
options = ENV['PARAMS'] ? Rack::Utils.parse_query(ENV['PARAMS']) : {}
|
7
|
+
models.each{|model| model.synchronize!(options)}
|
7
8
|
end
|
8
9
|
|
9
10
|
def extract_models
|
10
11
|
ENV['MODELS'].split(',').map(&:constantize)
|
11
12
|
end
|
12
|
-
|
13
|
-
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: HasRemote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sjoerd Andringa
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-25 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|