antonjenkins-model_sync 0.1.2 → 0.1.3

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.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/lib/model_sync.rb +28 -7
  3. data/model_sync.gemspec +2 -2
  4. metadata +2 -2
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.2') do |p|
5
+ Echoe.new('model_sync', '0.1.3') 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
@@ -24,16 +24,28 @@ module ModelSync
24
24
  def sync_changes
25
25
  # If we can find a slave instance...
26
26
  if slave_instance = find_slave_instance
27
- # ...then update all the attributes which we've mapped
28
- self.class.mappings.each do |source, dest|
29
- slave_instance.update_attribute(dest, self.read_attribute(source))
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
27
+ # ... then sync the changes over
28
+ perform_sync(slave_instance)
33
29
  end
34
30
  end
35
31
 
36
- private
32
+ def method_missing(id, *args, &block)
33
+ case(id.to_s)
34
+ when /^create_#{self.class.slave_model_name}$/
35
+ # If we've received a create_{slave_model} call then create a new instance of it and sync to it
36
+ new_instance = self.class.slave_model_class.new
37
+ perform_sync(new_instance)
38
+ # Save the new instance so that its primary key is generated and pass this value onto our master model
39
+ new_instance.save
40
+ self.update_attribute(self.class.relationship.keys.first, new_instance.read_attribute(self.class.relationship.values.first.to_s))
41
+ when /^synced_with_#{self.class.slave_model_name}\?$/
42
+ !!find_slave_instance
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ private
37
49
  def find_slave_instance
38
50
  # return nil if we don't have a value for the foreign key
39
51
  return nil unless foreign_key_value = self.read_attribute(self.class.relationship.keys.first)
@@ -41,6 +53,15 @@ private
41
53
  self.class.slave_model_class.find(:first,
42
54
  :conditions => "#{self.class.relationship.values.first.to_s} = #{foreign_key_value}")
43
55
  end
56
+
57
+ def perform_sync(slave_instance)
58
+ # Update all the attributes which we've mapped
59
+ self.class.mappings.each do |source, dest|
60
+ slave_instance.update_attribute(dest, self.read_attribute(source))
61
+ end
62
+ # Call the mapping_block if one is supplied
63
+ self.class.mapping_block.call(self, slave_instance) if self.class.mapping_block
64
+ end
44
65
  end
45
66
  end
46
67
 
data/model_sync.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{model_sync}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
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-07-06}
9
+ s.date = %q{2009-07-07}
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"]
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.2
4
+ version: 0.1.3
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-07-06 00:00:00 -07:00
12
+ date: 2009-07-07 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15