mongoid_deep_cloneable 0.0.1pre → 0.0.2pre
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.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/README.md +51 -3
- data/Rakefile +3 -0
- data/lib/mongoid_deep_cloneable/version.rb +1 -1
- data/lib/mongoid_deep_cloneable.rb +3 -3
- data/spec/factories/students.rb +1 -1
- data/spec/mongoid_deep_cloneable/cloning_spec.rb +7 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8c9afc01e8b2b88a8e1744f33122d0765e9de34
|
4
|
+
data.tar.gz: 500c44fec918e3ca2890d7f0389afd5b6f57c222
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 565268797bdfac9b0e5d1267b34150131e99b597fe9c482c2c13c81c8368704f2af7b1cfa4e6b8f486b4f12bc19fdd6c5fb2a30eb22c60337ba7a252719e7b69
|
7
|
+
data.tar.gz: ca73bebdd46ca34b1894493032cc57e6133337b97bc4f749a023e1df380518342abee7c73492a4d24f32f2e1704dd94436706c3b284366dfc1a0d2fd225d0385
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
|
1
|
+
[](https://travis-ci.org/KyleMacey/mongoid_deep_cloneable)
|
2
2
|
|
3
|
-
|
3
|
+
# mongoid_deep_cloneable
|
4
|
+
|
5
|
+
This is a gem for cloning a Mongoid object with its associations. Some ideas influenced by [moiristo/deep_cloneable](https://github.com/moiristo/deep_cloneable).
|
6
|
+
|
7
|
+
Currently, this gem is built to work with Mongoid 4 (developed out of necessity) but feel free to test against other versions of Mongoid.
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -18,7 +22,51 @@ Or install it yourself as:
|
|
18
22
|
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
|
25
|
+
Basic usage without arguments is the same as `#dup`
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
copy = object.deep_clone
|
29
|
+
copy.name == object.name
|
30
|
+
=> true
|
31
|
+
copy == object
|
32
|
+
=> false
|
33
|
+
```
|
34
|
+
|
35
|
+
You can also specify a child association to also be cloned.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
copy = object.deep_clone(include: :children)
|
39
|
+
copy.children.map(&:name) == object.children.map(&:name)
|
40
|
+
=> true
|
41
|
+
copy.children == object.children
|
42
|
+
=> false
|
43
|
+
```
|
44
|
+
|
45
|
+
You can also specify deeper nested associations, and arrays of associations
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
copy = object.deep_clone(include: { children: :granchildren})
|
49
|
+
|
50
|
+
copy = object.deep_clone(include: [:children, :other_children])
|
51
|
+
|
52
|
+
copy = object.deep_clone(include: [{ children: :grandchildren }, :other_children]
|
53
|
+
```
|
54
|
+
|
55
|
+
If you want to just "pirate" the associations from an object, you can specify a parent
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
copy = School.new name: "Beauxbatons"
|
59
|
+
school = School.find_by name: "Hogwarts"
|
60
|
+
school.students.map(&:name)
|
61
|
+
=> ["Harry Potter", "Ron Weasley", "Hermione Granger"]
|
62
|
+
|
63
|
+
copy = school.deep_clone(include: :students, parent: copy)
|
64
|
+
copy.name
|
65
|
+
=> "Beauxbatons"
|
66
|
+
|
67
|
+
copy.students.map(&:name)
|
68
|
+
=> ["Harry Potter", "Ron Weasley", "Hermione Granger"]
|
69
|
+
```
|
22
70
|
|
23
71
|
## Contributing
|
24
72
|
|
data/Rakefile
CHANGED
@@ -8,13 +8,13 @@ module MongoidDeepCloneable
|
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
10
10
|
def deep_clone options={}
|
11
|
-
copy = options.try(:[], :parent) || self.
|
11
|
+
copy = options.try(:[], :parent) || self.clone
|
12
12
|
if options && options[:include]
|
13
13
|
children_copy options[:include], copy
|
14
14
|
elsif copy.class.respond_to? :clone_associations
|
15
15
|
children_copy copy.class.clone_associations, copy
|
16
16
|
end
|
17
|
-
copy.save(validate: false)
|
17
|
+
copy.save(validate: false) unless options.try(:[], :build)
|
18
18
|
copy
|
19
19
|
end
|
20
20
|
|
@@ -31,7 +31,7 @@ private
|
|
31
31
|
end
|
32
32
|
associations.each do |association|
|
33
33
|
if association.is_a? Symbol
|
34
|
-
children = { include: options[association] } rescue nil
|
34
|
+
children = { include: options[association], build: true } rescue nil
|
35
35
|
if (val = self.send(association)).is_a? Array
|
36
36
|
parent.send("#{association}=", val.map{|x| x.deep_clone(children) })
|
37
37
|
else
|
data/spec/factories/students.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
FactoryGirl.define do
|
2
2
|
factory :student do
|
3
|
-
sequence :name, ["Harry Potter", "Ron Weasley", "Hermione
|
3
|
+
sequence :name, ["Harry Potter", "Ron Weasley", "Hermione Granger", "Hestia Carrow", "Nathaniel Douglas", "Fleur Delacour", "Gabrielle Delacour", "Gellert Grindelwald", "Viktor Krum"].cycle
|
4
4
|
school
|
5
5
|
|
6
6
|
after(:build) do |student|
|
@@ -33,4 +33,11 @@ describe MongoidDeepCloneable do
|
|
33
33
|
expect(copy.students.map(&:name)).to eq(subject.students.map(&:name))
|
34
34
|
expect(copy.name).to_not eq(subject.name)
|
35
35
|
end
|
36
|
+
|
37
|
+
it "should leave the original object alone" do
|
38
|
+
subject.save
|
39
|
+
student_count = subject.students.count
|
40
|
+
copy = subject.deep_clone(include: {students: [:books, :courses]})
|
41
|
+
expect(subject.reload.students.count).to eq(student_count)
|
42
|
+
end
|
36
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_deep_cloneable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle Macey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -103,6 +103,7 @@ extra_rdoc_files: []
|
|
103
103
|
files:
|
104
104
|
- .gitignore
|
105
105
|
- .rspec
|
106
|
+
- .travis.yml
|
106
107
|
- Gemfile
|
107
108
|
- LICENSE.txt
|
108
109
|
- README.md
|