deep_copy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +32 -0
- data/LICENSE +20 -0
- data/README.md +3 -0
- data/Rakefile +31 -0
- data/deep_copy.gemspec +28 -0
- data/lib/deep_copy.rb +61 -0
- data/test/test_helper.rb +0 -0
- metadata +92 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
deep_copy (0.0.1)
|
5
|
+
activerecord (>= 2.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.0)
|
11
|
+
activesupport (= 3.0.0)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.4.1)
|
14
|
+
activerecord (3.0.0)
|
15
|
+
activemodel (= 3.0.0)
|
16
|
+
activesupport (= 3.0.0)
|
17
|
+
arel (~> 1.0.0)
|
18
|
+
tzinfo (~> 0.3.23)
|
19
|
+
activesupport (3.0.0)
|
20
|
+
arel (1.0.1)
|
21
|
+
activesupport (~> 3.0.0)
|
22
|
+
builder (2.1.2)
|
23
|
+
i18n (0.4.1)
|
24
|
+
rake (0.8.7)
|
25
|
+
tzinfo (0.3.23)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
deep_copy!
|
32
|
+
rake
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Gonçalo Silva, Jan De Poorter
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
begin
|
2
|
+
require "bundler"
|
3
|
+
Bundler.setup
|
4
|
+
rescue LoadError
|
5
|
+
$stderr.puts "You need to have Bundler installed to be able build this gem."
|
6
|
+
end
|
7
|
+
|
8
|
+
gemspec = eval(File.read(Dir["*.gemspec"].first))
|
9
|
+
|
10
|
+
|
11
|
+
desc "Validate the gemspec"
|
12
|
+
task :gemspec do
|
13
|
+
gemspec.validate
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Build gem locally"
|
17
|
+
task :build => :gemspec do
|
18
|
+
system "gem build #{gemspec.name}.gemspec"
|
19
|
+
FileUtils.mkdir_p "pkg"
|
20
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Install gem locally"
|
24
|
+
task :install => :build do
|
25
|
+
system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Clean automatically generated files"
|
29
|
+
task :clean do
|
30
|
+
FileUtils.rm_rf "pkg"
|
31
|
+
end
|
data/deep_copy.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "deep_copy"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["Gonçalo Silva", "Jan De Poorter"]
|
8
|
+
s.email = ["goncalossilva@gmail.com", "github@defv.be"]
|
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 its associations"
|
12
|
+
s.rubyforge_project = s.name
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.7"
|
15
|
+
|
16
|
+
# If you have other dependencies, add them here
|
17
|
+
s.add_dependency "activerecord", ">= 2.3"
|
18
|
+
|
19
|
+
# If you need to check in files that aren't .rb files, add them here
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
22
|
+
s.extensions = `git ls-files ext/extconf.rb`.split("\n")
|
23
|
+
|
24
|
+
s.require_path = 'lib'
|
25
|
+
|
26
|
+
# For C extensions
|
27
|
+
# s.extensions = "ext/extconf.rb"
|
28
|
+
end
|
data/lib/deep_copy.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module DeepCopy
|
4
|
+
def deep_copy(options={})
|
5
|
+
kopy = clone
|
6
|
+
|
7
|
+
exceptions = options[:except]
|
8
|
+
inclusions = options[:include]
|
9
|
+
|
10
|
+
if exceptions
|
11
|
+
Array(exceptions).each do |attribute|
|
12
|
+
unless attribute.is_a?(Hash)
|
13
|
+
kopy.write_attribute(attribute, attributes_from_column_definition[attribute.to_s])
|
14
|
+
exceptions.delete(attribute)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
if inclusions
|
20
|
+
Array(inclusions).each do |association, deep_associations|
|
21
|
+
if(association.kind_of? Hash)
|
22
|
+
deep_associations = association[association.keys.first]
|
23
|
+
association = association.keys.first
|
24
|
+
end
|
25
|
+
|
26
|
+
puts "EXCEPTIONS: "
|
27
|
+
puts exceptions
|
28
|
+
puts association
|
29
|
+
|
30
|
+
deeper_exceptions = if exceptions.is_a?(Hash)
|
31
|
+
exceptions[association]
|
32
|
+
elsif exceptions.is_a?(Array)
|
33
|
+
exceptions.find { |el| el.keys.first == association}
|
34
|
+
end
|
35
|
+
|
36
|
+
opts = deep_associations.blank? ? {} : {:include => deep_associations}
|
37
|
+
opts.merge(:except => deeper_exceptions) unless deeper_exceptions.blank?
|
38
|
+
|
39
|
+
puts "OPTS: "
|
40
|
+
puts opts
|
41
|
+
|
42
|
+
association_reflection = self.class.reflect_on_association(association.to_sym)
|
43
|
+
cloned_object = case association_reflection.macro
|
44
|
+
when :belongs_to, :has_one
|
45
|
+
self.send(association) && self.send(association).deep_copy(opts)
|
46
|
+
when :has_many, :has_and_belongs_to_many
|
47
|
+
fk = association_reflection.options[:foreign_key] || self.class.to_s.underscore
|
48
|
+
self.send(association).collect { |obj| tmp = obj.deep_copy(opts)
|
49
|
+
tmp.send("#{fk}=", kopy)
|
50
|
+
tmp
|
51
|
+
}
|
52
|
+
end
|
53
|
+
kopy.send("#{association}=", cloned_object)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
return kopy
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
ActiveRecord::Base.send :include, DeepCopy
|
data/test/test_helper.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deep_copy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- "Gon\xC3\xA7alo Silva"
|
13
|
+
- Jan De Poorter
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-28 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 3
|
31
|
+
version: "2.3"
|
32
|
+
type: :runtime
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
description: A method to deep copy AR objects, including its associations
|
36
|
+
email:
|
37
|
+
- goncalossilva@gmail.com
|
38
|
+
- github@defv.be
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- CHANGELOG.md
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- deep_copy.gemspec
|
54
|
+
- lib/deep_copy.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/goncalossilva/deep_copy
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 325022159
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 1
|
81
|
+
- 3
|
82
|
+
- 7
|
83
|
+
version: 1.3.7
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project: deep_copy
|
87
|
+
rubygems_version: 1.3.7
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Deep copy for Active Record
|
91
|
+
test_files: []
|
92
|
+
|