deep_copy 0.0.1 → 0.0.2
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/Gemfile.lock +1 -1
- data/README.md +22 -1
- data/deep_copy.gemspec +3 -3
- data/lib/deep_copy.rb +9 -14
- metadata +6 -6
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,24 @@
|
|
1
|
-
Deep copy
|
1
|
+
# Deep copy
|
2
|
+
|
3
|
+
Deep copy allows you to clone Active Record objects including its associations.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Just require the "deep_copy" gem on your project. On Rails 2.3, that is:
|
8
|
+
config.gem "deep_copy"
|
9
|
+
on your environment.rb
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Pick any Active Record object and call _deep\_copy_ on it. You can also include associations and except fields, like this:
|
14
|
+
new_user = user.deep_copy :include => :car, :exclude => [:name, {:car => [:number_of_wheels, :number_of_accidents]}]
|
15
|
+
In this example we're cloning a user, including its car. However, we're not cloning the user's name nor are we cloning his car's _number\_of\_wheels_ and _number\_of\_accidents_.
|
16
|
+
|
17
|
+
You can also go deeper (bad pun intended). In this example, we'll clone a lot of people (excluding their names):
|
18
|
+
new_son = son.deep_copy :include => [{:parent => grand_parent}, :siblings], :exclude => [:name, {:parent => [:name, :grand_parent => :name]}, {:siblings => :name}]
|
19
|
+
|
20
|
+
### Acknowledgements
|
21
|
+
|
22
|
+
This gem was inspired in Jan De Poorter's [deep_cloning](http://github.com/DefV/deep_cloning) plugin. All of this work is his, except for the gem part and the field exceptions in associations.
|
2
23
|
|
3
24
|
Copyright (c) 2010 Gonçalo Silva
|
data/deep_copy.gemspec
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "deep_copy"
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.2"
|
6
6
|
s.platform = Gem::Platform::RUBY
|
7
7
|
s.authors = ["Gonçalo Silva", "Jan De Poorter"]
|
8
8
|
s.email = ["goncalossilva@gmail.com", "github@defv.be"]
|
9
9
|
s.homepage = "http://github.com/goncalossilva/deep_copy"
|
10
|
-
s.summary = "Deep copy for Active Record"
|
11
|
-
s.description = "A method to deep copy AR objects, including
|
10
|
+
s.summary = "Deep copy for Active Record objects"
|
11
|
+
s.description = "A method to deep copy AR objects, including their associations"
|
12
12
|
s.rubyforge_project = s.name
|
13
13
|
|
14
14
|
s.required_rubygems_version = ">= 1.3.7"
|
data/lib/deep_copy.rb
CHANGED
@@ -9,37 +9,32 @@ module DeepCopy
|
|
9
9
|
|
10
10
|
if exceptions
|
11
11
|
Array(exceptions).each do |attribute|
|
12
|
-
|
13
|
-
|
14
|
-
exceptions.delete(attribute)
|
15
|
-
end
|
12
|
+
kopy.write_attribute(attribute, attributes_from_column_definition[attribute.to_s])
|
13
|
+
#exceptions.delete(attribute) if exceptions.is_a?(Array)
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
17
|
if inclusions
|
20
18
|
Array(inclusions).each do |association, deep_associations|
|
21
|
-
if(association.
|
19
|
+
if(association.is_a?(Hash))
|
22
20
|
deep_associations = association[association.keys.first]
|
23
21
|
association = association.keys.first
|
24
22
|
end
|
25
|
-
|
26
|
-
puts "EXCEPTIONS: "
|
27
|
-
puts exceptions
|
28
|
-
puts association
|
23
|
+
association = association.to_sym
|
29
24
|
|
30
25
|
deeper_exceptions = if exceptions.is_a?(Hash)
|
31
26
|
exceptions[association]
|
32
27
|
elsif exceptions.is_a?(Array)
|
33
|
-
exceptions.find { |el| el.keys.first == association}
|
28
|
+
data = exceptions.find { |el| el.keys.first.to_sym == association if el.is_a?(Hash)}
|
29
|
+
data.nil? ? {} : data[association]
|
34
30
|
end
|
35
31
|
|
36
32
|
opts = deep_associations.blank? ? {} : {:include => deep_associations}
|
37
|
-
opts.merge(:except => deeper_exceptions) unless deeper_exceptions.blank?
|
33
|
+
opts.merge!(:except => deeper_exceptions) unless deeper_exceptions.blank?
|
38
34
|
|
39
|
-
|
40
|
-
|
35
|
+
association_reflection = self.class.reflect_on_association(association)
|
36
|
+
next if association_reflection.nil? # invalid association
|
41
37
|
|
42
|
-
association_reflection = self.class.reflect_on_association(association.to_sym)
|
43
38
|
cloned_object = case association_reflection.macro
|
44
39
|
when :belongs_to, :has_one
|
45
40
|
self.send(association) && self.send(association).deep_copy(opts)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Gon\xC3\xA7alo Silva"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-10-12 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
type: :runtime
|
33
33
|
prerelease: false
|
34
34
|
version_requirements: *id001
|
35
|
-
description: A method to deep copy AR objects, including
|
35
|
+
description: A method to deep copy AR objects, including their associations
|
36
36
|
email:
|
37
37
|
- goncalossilva@gmail.com
|
38
38
|
- github@defv.be
|
@@ -67,7 +67,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
67
|
requirements:
|
68
68
|
- - ">="
|
69
69
|
- !ruby/object:Gem::Version
|
70
|
-
hash:
|
70
|
+
hash: 529805745
|
71
71
|
segments:
|
72
72
|
- 0
|
73
73
|
version: "0"
|
@@ -87,6 +87,6 @@ rubyforge_project: deep_copy
|
|
87
87
|
rubygems_version: 1.3.7
|
88
88
|
signing_key:
|
89
89
|
specification_version: 3
|
90
|
-
summary: Deep copy for Active Record
|
90
|
+
summary: Deep copy for Active Record objects
|
91
91
|
test_files: []
|
92
92
|
|