mongoid_deep_cloneable 0.0.1pre → 0.0.2pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 256f34555eb5e4a3d4ffbea059637985fb5195db
4
- data.tar.gz: 87154a9399a0b0bee985c77be6e7f8e05d940b88
3
+ metadata.gz: f8c9afc01e8b2b88a8e1744f33122d0765e9de34
4
+ data.tar.gz: 500c44fec918e3ca2890d7f0389afd5b6f57c222
5
5
  SHA512:
6
- metadata.gz: 1b93e0678e2f8370d0cbbd8e4fa2ce7f1b1e2ad08c7bc1243b2c5a0708031274ed1f423993abac8da9be47f87f0030514c9a538fed556417e82264636bb81b54
7
- data.tar.gz: e3d264976209a57ca5613c0db86e1a49e787c002cf27473b2c9e86260194aead39192dfc524b81ed1aeff48471354aab246e478c1d8585db670f893c4143de8f
6
+ metadata.gz: 565268797bdfac9b0e5d1267b34150131e99b597fe9c482c2c13c81c8368704f2af7b1cfa4e6b8f486b4f12bc19fdd6c5fb2a30eb22c60337ba7a252719e7b69
7
+ data.tar.gz: ca73bebdd46ca34b1894493032cc57e6133337b97bc4f749a023e1df380518342abee7c73492a4d24f32f2e1704dd94436706c3b284366dfc1a0d2fd225d0385
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - ruby-head
5
+ services: mongodb
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
- # MongoidDeepCloneable
1
+ [![Build Status](https://travis-ci.org/KyleMacey/mongoid_deep_cloneable.png?branch=master)](https://travis-ci.org/KyleMacey/mongoid_deep_cloneable)
2
2
 
3
- TODO: Write a gem description
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
- TODO: Write usage instructions here
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
@@ -1 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
@@ -1,3 +1,3 @@
1
1
  module MongoidDeepCloneable
2
- VERSION = "0.0.1pre"
2
+ VERSION = "0.0.2pre"
3
3
  end
@@ -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.dup
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
@@ -1,6 +1,6 @@
1
1
  FactoryGirl.define do
2
2
  factory :student do
3
- sequence :name, ["Harry Potter", "Ron Weasley", "Hermione Ginger", "Hestia Carrow", "Nathaniel Douglas", "Fleur Delacour", "Gabrielle Delacour", "Gellert Grindelwald", "Viktor Krum"].cycle
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.1pre
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: 2013-12-31 00:00:00.000000000 Z
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