deep_cloneable 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails'
6
+ gem 'jeweler'
7
+
8
+ group :test do
9
+ gem 'sqlite3'
10
+ end
@@ -0,0 +1,100 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ deep_cloneable (1.3.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ actionmailer (3.2.2)
10
+ actionpack (= 3.2.2)
11
+ mail (~> 2.4.0)
12
+ actionpack (3.2.2)
13
+ activemodel (= 3.2.2)
14
+ activesupport (= 3.2.2)
15
+ builder (~> 3.0.0)
16
+ erubis (~> 2.7.0)
17
+ journey (~> 1.0.1)
18
+ rack (~> 1.4.0)
19
+ rack-cache (~> 1.1)
20
+ rack-test (~> 0.6.1)
21
+ sprockets (~> 2.1.2)
22
+ activemodel (3.2.2)
23
+ activesupport (= 3.2.2)
24
+ builder (~> 3.0.0)
25
+ activerecord (3.2.2)
26
+ activemodel (= 3.2.2)
27
+ activesupport (= 3.2.2)
28
+ arel (~> 3.0.2)
29
+ tzinfo (~> 0.3.29)
30
+ activeresource (3.2.2)
31
+ activemodel (= 3.2.2)
32
+ activesupport (= 3.2.2)
33
+ activesupport (3.2.2)
34
+ i18n (~> 0.6)
35
+ multi_json (~> 1.0)
36
+ arel (3.0.2)
37
+ builder (3.0.0)
38
+ erubis (2.7.0)
39
+ git (1.2.5)
40
+ hike (1.2.1)
41
+ i18n (0.6.0)
42
+ jeweler (1.8.3)
43
+ bundler (~> 1.0)
44
+ git (>= 1.2.5)
45
+ rake
46
+ rdoc
47
+ journey (1.0.3)
48
+ json (1.6.6)
49
+ mail (2.4.4)
50
+ i18n (>= 0.4.0)
51
+ mime-types (~> 1.16)
52
+ treetop (~> 1.4.8)
53
+ mime-types (1.18)
54
+ multi_json (1.2.0)
55
+ polyglot (0.3.3)
56
+ rack (1.4.1)
57
+ rack-cache (1.2)
58
+ rack (>= 0.4)
59
+ rack-ssl (1.3.2)
60
+ rack
61
+ rack-test (0.6.1)
62
+ rack (>= 1.0)
63
+ rails (3.2.2)
64
+ actionmailer (= 3.2.2)
65
+ actionpack (= 3.2.2)
66
+ activerecord (= 3.2.2)
67
+ activeresource (= 3.2.2)
68
+ activesupport (= 3.2.2)
69
+ bundler (~> 1.0)
70
+ railties (= 3.2.2)
71
+ railties (3.2.2)
72
+ actionpack (= 3.2.2)
73
+ activesupport (= 3.2.2)
74
+ rack-ssl (~> 1.3.2)
75
+ rake (>= 0.8.7)
76
+ rdoc (~> 3.4)
77
+ thor (~> 0.14.6)
78
+ rake (0.9.2.2)
79
+ rdoc (3.12)
80
+ json (~> 1.4)
81
+ sprockets (2.1.2)
82
+ hike (~> 1.2)
83
+ rack (~> 1.0)
84
+ tilt (~> 1.1, != 1.3.0)
85
+ sqlite3 (1.3.5)
86
+ thor (0.14.6)
87
+ tilt (1.3.3)
88
+ treetop (1.4.10)
89
+ polyglot
90
+ polyglot (>= 0.3.1)
91
+ tzinfo (0.3.32)
92
+
93
+ PLATFORMS
94
+ ruby
95
+
96
+ DEPENDENCIES
97
+ deep_cloneable!
98
+ jeweler
99
+ rails
100
+ sqlite3
@@ -50,11 +50,18 @@ If this is not an option for you, it is also possible to populate the dictionary
50
50
  === Cloning a model without an attribute or nested multiple attributes
51
51
  pirate.dup :include => :parrot, :except => [:name, { :parrot => [:name] }]
52
52
 
53
+ === Cloning with a block
54
+ pirate.dup :include => :parrot do |original, kopy|
55
+ kopy.cloned_from_id = original.id if kopy.respond_to?(:cloned_from_id)
56
+ end
57
+
53
58
  == Contributors
54
59
 
55
60
  * Michael He
56
61
  * Indrek Juhkam
57
62
  * Mart Karu
63
+ * Rolf Timmermans
64
+ * Ilya Kuzmin
58
65
 
59
66
  == Note on Patches/Pull Requests
60
67
 
data/Rakefile CHANGED
@@ -37,8 +37,6 @@ rescue LoadError
37
37
  end
38
38
  end
39
39
 
40
- task :test => :check_dependencies
41
-
42
40
  task :default => :test
43
41
 
44
42
  require 'rake/rdoctask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 1.4.0
@@ -4,20 +4,22 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{deep_cloneable}
8
- s.version = "1.3.1"
7
+ s.name = "deep_cloneable"
8
+ s.version = "1.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Reinier de Lange}]
12
- s.date = %q{2012-01-26}
13
- s.description = %q{Extends the functionality of ActiveRecord::Base#clone to perform a deep clone that includes user specified associations. }
14
- s.email = %q{r.j.delange@nedforce.nl}
11
+ s.authors = ["Reinier de Lange"]
12
+ s.date = "2012-04-02"
13
+ s.description = "Extends the functionality of ActiveRecord::Base#clone to perform a deep clone that includes user specified associations. "
14
+ s.email = "r.j.delange@nedforce.nl"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
21
23
  "LICENSE",
22
24
  "README.rdoc",
23
25
  "Rakefile",
@@ -30,18 +32,27 @@ Gem::Specification.new do |s|
30
32
  "test/test_deep_cloneable.rb",
31
33
  "test/test_helper.rb"
32
34
  ]
33
- s.homepage = %q{http://github.com/moiristo/deep_cloneable}
34
- s.require_paths = [%q{lib}]
35
- s.rubygems_version = %q{1.8.6}
36
- s.summary = %q{This gem gives every ActiveRecord::Base object the possibility to do a deep clone.}
35
+ s.homepage = "http://github.com/moiristo/deep_cloneable"
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "1.8.21"
38
+ s.summary = "This gem gives every ActiveRecord::Base object the possibility to do a deep clone."
37
39
 
38
40
  if s.respond_to? :specification_version then
39
41
  s.specification_version = 3
40
42
 
41
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<deep_cloneable>, [">= 0"])
45
+ s.add_runtime_dependency(%q<rails>, [">= 0"])
46
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
42
47
  else
48
+ s.add_dependency(%q<deep_cloneable>, [">= 0"])
49
+ s.add_dependency(%q<rails>, [">= 0"])
50
+ s.add_dependency(%q<jeweler>, [">= 0"])
43
51
  end
44
52
  else
53
+ s.add_dependency(%q<deep_cloneable>, [">= 0"])
54
+ s.add_dependency(%q<rails>, [">= 0"])
55
+ s.add_dependency(%q<jeweler>, [">= 0"])
45
56
  end
46
57
  end
47
58
 
@@ -1,7 +1,24 @@
1
1
  class ActiveRecord::Base
2
2
  module DeepCloneable
3
3
  @@rails31 = ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR > 0
4
-
4
+
5
+ # ActiveRecord::Base has its own dup method for Ruby 1.8.7. We have to
6
+ # redefine it and put it in a module so that we can override it in a
7
+ # module and call the original with super().
8
+ if @@rails31 and !Object.respond_to? :initialize_dup
9
+ ActiveRecord::Base.class_eval do
10
+ module Dup
11
+ def dup
12
+ copy = super
13
+ copy.initialize_dup(self)
14
+ copy
15
+ end
16
+ end
17
+ remove_method :dup
18
+ include Dup
19
+ end
20
+ end
21
+
5
22
  # clones an ActiveRecord model.
6
23
  # if passed the :include option, it will deep clone the given associations
7
24
  # if passed the :except option, it won't clone the given attributes
@@ -42,7 +59,7 @@ class ActiveRecord::Base
42
59
  # ==== Cloning a model without an attribute or nested multiple attributes
43
60
  # pirate.clone :include => :parrot, :except => [:name, { :parrot => [:name] }]
44
61
  #
45
- define_method (@@rails31 ? :dup : :clone) do |*args|
62
+ define_method (@@rails31 ? :dup : :clone) do |*args, &block|
46
63
  options = args[0] || {}
47
64
 
48
65
  dict = options[:dictionary]
@@ -56,6 +73,8 @@ class ActiveRecord::Base
56
73
  dict[tableized_class][self] ||= super()
57
74
  end
58
75
 
76
+ block.call(self, kopy) if block
77
+
59
78
  deep_exceptions = {}
60
79
  if options[:except]
61
80
  exceptions = options[:except].nil? ? [] : [options[:except]].flatten
@@ -81,7 +100,7 @@ class ActiveRecord::Base
81
100
 
82
101
  cloned_object = case association_reflection.macro
83
102
  when :belongs_to, :has_one
84
- self.send(association) && self.send(association).send(__method__, opts)
103
+ self.send(association) && self.send(association).send(__method__, opts, &block)
85
104
  when :has_many, :has_and_belongs_to_many
86
105
  primary_key_name = (@@rails31 ? association_reflection.foreign_key : association_reflection.primary_key_name).to_s
87
106
 
@@ -90,7 +109,7 @@ class ActiveRecord::Base
90
109
  end.try(:name)
91
110
 
92
111
  self.send(association).collect do |obj|
93
- tmp = obj.send(__method__, opts)
112
+ tmp = obj.send(__method__, opts, &block)
94
113
  tmp.send("#{primary_key_name}=", nil)
95
114
  tmp.send("#{reverse_association_name.to_s}=", kopy) if reverse_association_name
96
115
  tmp
@@ -150,4 +150,13 @@ class TestDeepCloneable < Test::Unit::TestCase
150
150
  assert_equal 2, dup_human.chickens.count
151
151
  end
152
152
 
153
+ def test_should_dup_with_block
154
+ dup = @jack.send(@@clone_method, :include => :parrot) do |original, kopy|
155
+ kopy.cloned_from_id = original.id
156
+ end
157
+
158
+ assert dup.save
159
+ assert_equal @jack.id, dup.cloned_from_id
160
+ assert_equal @jack.parrot.id, dup.parrot.cloned_from_id
161
+ end
153
162
  end
@@ -34,7 +34,7 @@ end
34
34
 
35
35
  class GoldPiece < ActiveRecord::Base; belongs_to :treasure end
36
36
  class Matey < ActiveRecord::Base; belongs_to :pirate end
37
- class Parrot < ActiveRecord::Base; belongs_to :pirate end
37
+ class Parrot < ActiveRecord::Base; belongs_to :pirate; attr_accessor :cloned_from_id end
38
38
  class BattleShip < ActiveRecord::Base; has_many :pirates, :as => :ship end
39
39
 
40
40
  class Pirate < ActiveRecord::Base
@@ -44,6 +44,8 @@ class Pirate < ActiveRecord::Base
44
44
  has_many :treasures, :foreign_key => 'owner'
45
45
  has_many :gold_pieces, :through => :treasures
46
46
  has_one :parrot
47
+
48
+ attr_accessor :cloned_from_id
47
49
  end
48
50
 
49
51
  class Treasure < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_cloneable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-26 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-04-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: deep_cloneable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
14
62
  description: ! 'Extends the functionality of ActiveRecord::Base#clone to perform a
15
63
  deep clone that includes user specified associations. '
16
64
  email: r.j.delange@nedforce.nl
@@ -21,6 +69,8 @@ extra_rdoc_files:
21
69
  - README.rdoc
22
70
  files:
23
71
  - .document
72
+ - Gemfile
73
+ - Gemfile.lock
24
74
  - LICENSE
25
75
  - README.rdoc
26
76
  - Rakefile
@@ -44,6 +94,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
94
  - - ! '>='
45
95
  - !ruby/object:Gem::Version
46
96
  version: '0'
97
+ segments:
98
+ - 0
99
+ hash: -1579513982850858765
47
100
  required_rubygems_version: !ruby/object:Gem::Requirement
48
101
  none: false
49
102
  requirements:
@@ -52,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
105
  version: '0'
53
106
  requirements: []
54
107
  rubyforge_project:
55
- rubygems_version: 1.8.6
108
+ rubygems_version: 1.8.21
56
109
  signing_key:
57
110
  specification_version: 3
58
111
  summary: This gem gives every ActiveRecord::Base object the possibility to do a deep