antonjenkins-model_sync 0.1.0

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/Manifest ADDED
@@ -0,0 +1,4 @@
1
+ lib/model_sync.rb
2
+ Rakefile
3
+ README.rdoc
4
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = model_sync
2
+
3
+ model_sync is a simple gem for pushing changes to one of your rails models to another.
4
+
5
+ class User < ActiveRecord::Base
6
+ model_sync :sync_to => :student,
7
+ :relationship => { :student_id => :s_stno },
8
+ :mappings => { :forename => :s_forename }
9
+ end
10
+
11
+ 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
+ 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
+ == Why use it?
16
+
17
+ 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.
18
+
19
+ Once a user books a course and becomes a student as well we've got a user and a student record we need to keep in sync because the user could change their address through the website, for example, which then needs feeding into the main system.
20
+
21
+ The model_sync gem will do all the syncing for me in a totally transparent way.
22
+
23
+ == Limitations
24
+
25
+ Currently the changes are pushed over with an after_save hook using update_attribute on the :sync_to model. This bypasses validations and any failures on update_attribute are currently not handled.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('model_sync', '0.1.0') do |p|
6
+ p.description = "Sync changes to an ActiveRecord model to another model"
7
+ p.url = "http://github.com/antonjenkins/model_sync"
8
+ p.author = "Anton Jenkins"
9
+ p.email = "info@pixellatedvisions.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/lib/model_sync.rb ADDED
@@ -0,0 +1,47 @@
1
+ module ModelSync
2
+ module Base
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ extend Config
6
+ end
7
+ end
8
+
9
+ module Config
10
+ attr_reader :slave_model_name, :slave_model_class, :relationship, :mappings
11
+
12
+ def model_sync(options)
13
+ @slave_model_name = options[:sync_to].to_s.downcase
14
+ @slave_model_class = Kernel.const_get(@slave_model_name.classify)
15
+ @relationship = options[:relationship]
16
+ @mappings = options[:mappings]
17
+
18
+ # Add a callback to sync_changes on every save
19
+ self.after_save :sync_changes
20
+ end
21
+ end
22
+
23
+ def sync_changes
24
+ # If we can find a slave instance...
25
+ if slave_instance = find_slave_instance
26
+ # ... then update all the attributes which we've mapped
27
+ self.class.mappings.each do |source, dest|
28
+ slave_instance.update_attribute(dest, self.read_attribute(source))
29
+ end
30
+ end
31
+ end
32
+
33
+ private
34
+ def find_slave_instance
35
+ self.class.slave_model_class.find(:first,
36
+ :conditions => "#{self.class.relationship.values.first.to_s} = #{self.read_attribute(self.class.relationship.keys.first)}")
37
+ end
38
+ end
39
+ end
40
+
41
+ if defined?(::ActiveRecord)
42
+ module ::ActiveRecord
43
+ class Base
44
+ include ModelSync::Base
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{model_sync}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Anton Jenkins"]
9
+ s.date = %q{2009-06-30}
10
+ s.description = %q{Sync changes to an ActiveRecord model to another model}
11
+ s.email = %q{info@pixellatedvisions.com}
12
+ s.extra_rdoc_files = ["lib/model_sync.rb", "README.rdoc"]
13
+ s.files = ["lib/model_sync.rb", "Rakefile", "README.rdoc", "Manifest", "model_sync.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/antonjenkins/model_sync}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Model_sync", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{model_sync}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Sync changes to an ActiveRecord model to another model}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: antonjenkins-model_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Jenkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Sync changes to an ActiveRecord model to another model
17
+ email: info@pixellatedvisions.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/model_sync.rb
24
+ - README.rdoc
25
+ files:
26
+ - lib/model_sync.rb
27
+ - Rakefile
28
+ - README.rdoc
29
+ - Manifest
30
+ - model_sync.gemspec
31
+ has_rdoc: true
32
+ homepage: http://github.com/antonjenkins/model_sync
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --line-numbers
36
+ - --inline-source
37
+ - --title
38
+ - Model_sync
39
+ - --main
40
+ - README.rdoc
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "1.2"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: model_sync
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Sync changes to an ActiveRecord model to another model
62
+ test_files: []
63
+