antonjenkins-model_sync 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -1
- data/Rakefile +1 -1
- data/lib/model_sync.rb +4 -1
- data/model_sync.gemspec +4 -5
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -5,13 +5,16 @@ model_sync is a simple gem for pushing changes to one of your rails models to an
|
|
5
5
|
class User < ActiveRecord::Base
|
6
6
|
model_sync :sync_to => :student,
|
7
7
|
:relationship => { :student_id => :s_stno },
|
8
|
-
:mappings => { :forename => :s_forename }
|
8
|
+
:mappings => { :forename => :s_forename },
|
9
|
+
:mapping_block => Proc.new { |master, slave| slave.update_attribute(:s_sex, master.gender ? master.gender.code : nil) }
|
9
10
|
end
|
10
11
|
|
11
12
|
Assuming we have a Student model, the above code will add an after_save callback to the User model which will push changes to the forename value into the s_forename value of Student. The correct Student instance is found using the :relationship hash - student_id in User must equal s_stno in Student.
|
12
13
|
|
13
14
|
Although the above example only pushes changes to forename, you can add more mappings to the :mappings hash to push over as many values as you like.
|
14
15
|
|
16
|
+
When things are a bit more complicated than a simple like for like mapping you can use the :mapping_block option to pass a block which has access to the master and slave objects. Using these objects you can perform more complicated tasks than just making one value equal another.
|
17
|
+
|
15
18
|
== Why use it?
|
16
19
|
|
17
20
|
I developed this gem as part of a web front end which I'm building for an old legacy system. The legacy system is a student records system which holds students which are booked on courses. However I don't want to add users of the website into the system as students until they have actually booked a course. So there needs to be a certain amount of separation between the online element and the legacy system, mainly because I can't make changes to the database structure of the legacy system.
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('model_sync', '0.1.
|
5
|
+
Echoe.new('model_sync', '0.1.1') do |p|
|
6
6
|
p.description = "Sync changes to an ActiveRecord model to another model"
|
7
7
|
p.url = "http://github.com/antonjenkins/model_sync"
|
8
8
|
p.author = "Anton Jenkins"
|
data/lib/model_sync.rb
CHANGED
@@ -7,13 +7,14 @@ module ModelSync
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module Config
|
10
|
-
attr_reader :slave_model_name, :slave_model_class, :relationship, :mappings
|
10
|
+
attr_reader :slave_model_name, :slave_model_class, :relationship, :mappings, :mapping_block
|
11
11
|
|
12
12
|
def model_sync(options)
|
13
13
|
@slave_model_name = options[:sync_to].to_s.downcase
|
14
14
|
@slave_model_class = Kernel.const_get(@slave_model_name.classify)
|
15
15
|
@relationship = options[:relationship]
|
16
16
|
@mappings = options[:mappings]
|
17
|
+
@mapping_block = options[:mapping_block]
|
17
18
|
|
18
19
|
# Add a callback to sync_changes on every save
|
19
20
|
self.after_save :sync_changes
|
@@ -27,6 +28,8 @@ module ModelSync
|
|
27
28
|
self.class.mappings.each do |source, dest|
|
28
29
|
slave_instance.update_attribute(dest, self.read_attribute(source))
|
29
30
|
end
|
31
|
+
# Call the mapping_block if one is supplied
|
32
|
+
self.class.mapping_block.call(self, slave_instance) if self.class.mapping_block
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
data/model_sync.gemspec
CHANGED
@@ -2,26 +2,25 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{model_sync}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Anton Jenkins"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-07-02}
|
10
10
|
s.description = %q{Sync changes to an ActiveRecord model to another model}
|
11
11
|
s.email = %q{info@pixellatedvisions.com}
|
12
12
|
s.extra_rdoc_files = ["lib/model_sync.rb", "README.rdoc"]
|
13
13
|
s.files = ["lib/model_sync.rb", "Rakefile", "README.rdoc", "Manifest", "model_sync.gemspec"]
|
14
|
-
s.has_rdoc = true
|
15
14
|
s.homepage = %q{http://github.com/antonjenkins/model_sync}
|
16
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Model_sync", "--main", "README.rdoc"]
|
17
16
|
s.require_paths = ["lib"]
|
18
17
|
s.rubyforge_project = %q{model_sync}
|
19
|
-
s.rubygems_version = %q{1.3.
|
18
|
+
s.rubygems_version = %q{1.3.4}
|
20
19
|
s.summary = %q{Sync changes to an ActiveRecord model to another model}
|
21
20
|
|
22
21
|
if s.respond_to? :specification_version then
|
23
22
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
-
s.specification_version =
|
23
|
+
s.specification_version = 3
|
25
24
|
|
26
25
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
26
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: antonjenkins-model_sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Jenkins
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-02 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -28,7 +28,7 @@ files:
|
|
28
28
|
- README.rdoc
|
29
29
|
- Manifest
|
30
30
|
- model_sync.gemspec
|
31
|
-
has_rdoc:
|
31
|
+
has_rdoc: false
|
32
32
|
homepage: http://github.com/antonjenkins/model_sync
|
33
33
|
post_install_message:
|
34
34
|
rdoc_options:
|
@@ -57,7 +57,7 @@ requirements: []
|
|
57
57
|
rubyforge_project: model_sync
|
58
58
|
rubygems_version: 1.2.0
|
59
59
|
signing_key:
|
60
|
-
specification_version:
|
60
|
+
specification_version: 3
|
61
61
|
summary: Sync changes to an ActiveRecord model to another model
|
62
62
|
test_files: []
|
63
63
|
|