deep_cloning 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +28 -0
  3. data/lib/deep_cloning.rb +61 -0
  4. metadata +48 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 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.
@@ -0,0 +1,28 @@
1
+ = Deep Cloning Plugin
2
+
3
+ This plugin gives every ActiveRecord::Base object the possibility to do a deep clone.
4
+
5
+ Repository is on github: http://github.com/DefV/deep_cloning/tree/master
6
+
7
+ Install with:
8
+ script/plugin install git://github.com/DefV/deep_cloning.git
9
+
10
+ == Example
11
+
12
+ === Cloning a model without an attribute
13
+ pirate.dup :except => :name
14
+
15
+ === Cloning a model without multiple attributes
16
+ pirate.dup :except => [:name, :nick_name]
17
+ === Cloning one single association
18
+ pirate.dup :include => :mateys
19
+
20
+ === Cloning multiple associations
21
+ pirate.dup :include => [:mateys, :treasures]
22
+
23
+ === Cloning really deep
24
+ pirate.dup :include => {:treasures => :gold_pieces}
25
+
26
+ === Cloning really deep with multiple associations
27
+ pirate.dup :include => [:mateys, {:treasures => :gold_pieces}]
28
+ Copyright (c) 2008 Jan De Poorter, released under the MIT license
@@ -0,0 +1,61 @@
1
+ # DeepCloning
2
+
3
+ module DeepCloning
4
+ def self.included(base) #:nodoc:
5
+ base.alias_method_chain :dup, :deep_cloning
6
+ end
7
+
8
+ # dups an ActiveRecord model.
9
+ # if passed the :include option, it will deep dup the given associations
10
+ # if passed the :except option, it won't dup the given attributes
11
+ #
12
+ # === Usage:
13
+ #
14
+ # ==== Cloning a model without an attribute
15
+ # pirate.dup :except => :name
16
+ #
17
+ # ==== Cloning a model without multiple attributes
18
+ # pirate.dup :except => [:name, :nick_name]
19
+ # ==== Cloning one single association
20
+ # pirate.dup :include => :mateys
21
+ #
22
+ # ==== Cloning multiple associations
23
+ # pirate.dup :include => [:mateys, :treasures]
24
+ #
25
+ # ==== Cloning really deep
26
+ # pirate.dup :include => {:treasures => :gold_pieces}
27
+ #
28
+ # ==== Cloning really deep with multiple associations
29
+ # pirate.dup :include => [:mateys, {:treasures => :gold_pieces}]
30
+ #
31
+ def dup_with_deep_cloning options = {}
32
+ kopy = dup_without_deep_cloning()
33
+
34
+ if options[:except]
35
+ Array(options[:except]).each do |attribute|
36
+ kopy.send("#{attribute}=", self.class.column_defaults[attribute.to_s])
37
+ end
38
+ end
39
+
40
+ if options[:include]
41
+ Array(options[:include]).each do |association, deep_associations|
42
+ if (association.kind_of? Hash)
43
+ deep_associations = association[association.keys.first]
44
+ association = association.keys.first
45
+ end
46
+ opts = deep_associations.blank? ? {} : {:include => deep_associations}
47
+ cloned_object = case self.class.reflect_on_association(association).macro
48
+ when :belongs_to, :has_one
49
+ self.send(association) && self.send(association).dup(opts)
50
+ when :has_many, :has_and_belongs_to_many
51
+ self.send(association).collect { |obj| obj.dup(opts) }
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, DeepCloning
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deep_cloning
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan De Poorter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Allows you to dup an ActiveRecord object with selectively keeping attributes
15
+ / associations with :include, :exclude, ...
16
+ email: jan@sumocoders.be
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/deep_cloning.rb
22
+ - MIT-LICENSE
23
+ - README.rdoc
24
+ homepage: https://github.com/defv/deep_cloning
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.15
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Easily duplicate ActiveRecord objects
48
+ test_files: []