mongoid_deep_cloneable 0.0.1pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 256f34555eb5e4a3d4ffbea059637985fb5195db
4
+ data.tar.gz: 87154a9399a0b0bee985c77be6e7f8e05d940b88
5
+ SHA512:
6
+ metadata.gz: 1b93e0678e2f8370d0cbbd8e4fa2ce7f1b1e2ad08c7bc1243b2c5a0708031274ed1f423993abac8da9be47f87f0030514c9a538fed556417e82264636bb81b54
7
+ data.tar.gz: e3d264976209a57ca5613c0db86e1a49e787c002cf27473b2c9e86260194aead39192dfc524b81ed1aeff48471354aab246e478c1d8585db670f893c4143de8f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_deep_cloneable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kyle Macey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MongoidDeepCloneable
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mongoid_deep_cloneable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mongoid_deep_cloneable
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module MongoidDeepCloneable
2
+ VERSION = "0.0.1pre"
3
+ end
@@ -0,0 +1,45 @@
1
+ require "mongoid_deep_cloneable/version"
2
+
3
+ Mongoid::Document.class_eval do
4
+ include MongoidDeepCloneable
5
+ end
6
+
7
+ module MongoidDeepCloneable
8
+ extend ActiveSupport::Concern
9
+
10
+ def deep_clone options={}
11
+ copy = options.try(:[], :parent) || self.dup
12
+ if options && options[:include]
13
+ children_copy options[:include], copy
14
+ elsif copy.class.respond_to? :clone_associations
15
+ children_copy copy.class.clone_associations, copy
16
+ end
17
+ copy.save(validate: false)
18
+ copy
19
+ end
20
+
21
+ private
22
+ def children_copy options, parent
23
+ if options.is_a? Hash
24
+ associations = options.keys.collect &:to_sym
25
+ elsif options.is_a? Array
26
+ associations = options.flatten
27
+ elsif options.is_a? Symbol
28
+ associations = [options]
29
+ else
30
+ raise ArgumentError, "invalid input type for associations. Acceptable input types are Hash, Array, or Symbol"
31
+ end
32
+ associations.each do |association|
33
+ if association.is_a? Symbol
34
+ children = { include: options[association] } rescue nil
35
+ if (val = self.send(association)).is_a? Array
36
+ parent.send("#{association}=", val.map{|x| x.deep_clone(children) })
37
+ else
38
+ parent.send("#{association}=", val)
39
+ end
40
+ elsif association.is_a? Hash
41
+ children_copy association, parent
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid_deep_cloneable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid_deep_cloneable"
8
+ spec.version = MongoidDeepCloneable::VERSION
9
+ spec.authors = ["Kyle Macey"]
10
+ spec.email = ["shout@kylemacey.com"]
11
+ spec.description = %q{A gem used to clone a Mongoid object with its associations}
12
+ spec.summary = %q{A gem used to clone a Mongoid object with its associations}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = Dir["spec/**/*"]
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec-rails"
24
+ spec.add_development_dependency "pry-rails"
25
+ spec.add_development_dependency "factory_girl_rails", "~> 4.0"
26
+ spec.add_dependency "mongoid", "~> 4.0.0.alpha1"
27
+ end
@@ -0,0 +1,10 @@
1
+ FactoryGirl.define do
2
+ factory :book do
3
+ sequence :title, ["The Standard Book of Spells", "Achievements in Charming", "Curses and Counter-Curses", "Jinxes for the Jinxed", "Secrets of the Darkest Art", "Saucy Tricks for Tricky Sorts", "Easy Spells to Fool Muggles", "Theories of Transubstantial Transfiguration", "Self-Defensive Spellwork"].cycle
4
+ published_on { (rand(1..5000).years + rand(365).days).ago }
5
+ student
6
+ after(:build) do |book|
7
+ rand(1..5).times { book.spells << create(:spell, book: book) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :course do
3
+ sequence :name, ["Astronomy", "Charms", "Dark Arts", "Flying", "Herbology", "Potions", "Transfiguration"].cycle
4
+ student
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ FactoryGirl.define do
2
+ factory :school do
3
+ sequence :name, %w[Hogwarts Beauxbatons Durmstrang].cycle
4
+ after(:build) do |school|
5
+ rand(1..5).times { school.students << create(:student, school: school) }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :spell do
3
+ sequence :name, ["Accio", "Age Line", "Anti-Jinx", "Cistem Aperio", "Cribbing Spell", "Flagrate", "Inflatus", "Magicus Extremos", "Reducio", "Riddikulus", "Serpensortia", "Ventus"].cycle
4
+ book
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ FactoryGirl.define do
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
4
+ school
5
+
6
+ after(:build) do |student|
7
+ rand(1..5).times { student.books << create(:book, student: student) }
8
+ rand(1..5).times { student.courses << create(:course, student: student) }
9
+ # student.wand = create(:wand, student: student)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ FactoryGirl.define do
2
+ factory :wand do
3
+ sequence :color, ["Red", "Blue", "Green", "Black", "Orange", "White"].cycle
4
+ sequence :wood, ["Holly", "Vine", "Hornbeam", "Hawthorn", "Yew", "Ash"].cycle
5
+ inches { rand(100..150)/10 }
6
+ association :student
7
+ end
8
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ describe MongoidDeepCloneable do
3
+ subject { FactoryGirl.build(:school) }
4
+
5
+ it "should attach its method to existing models" do
6
+ expect(subject).to respond_to(:deep_clone)
7
+ end
8
+
9
+ it "should make a clone of an object" do
10
+ copy = subject.deep_clone
11
+ expect(copy.name).to eq(subject.name)
12
+ end
13
+
14
+ it "should be able to clone an object with its children" do
15
+ copy = subject.deep_clone(include: :students)
16
+ expect(copy.students.map(&:name)).to eq(subject.students.map(&:name))
17
+ end
18
+
19
+ it "should be able to clone two levels deep" do
20
+ copy = subject.deep_clone(include: {students: :books})
21
+ expect(copy.students.map(&:books).flatten.map(&:title)).to eq(subject.students.map(&:books).flatten.map(&:title))
22
+ end
23
+
24
+ it "should be able to clone multiple associations" do
25
+ copy = subject.deep_clone(include: {students: [:books, :courses]})
26
+ expect(copy.students.map(&:books).flatten.map(&:title)).to eq(subject.students.map(&:books).flatten.map(&:title))
27
+ expect(copy.students.map(&:courses).flatten.map(&:name)).to eq(subject.students.map(&:courses).flatten.map(&:name))
28
+ end
29
+
30
+ it "should be able to pirate associations from another object" do
31
+ copy = FactoryGirl.build(:school)
32
+ copy = subject.deep_clone(include: :students, parent: copy)
33
+ expect(copy.students.map(&:name)).to eq(subject.students.map(&:name))
34
+ expect(copy.name).to_not eq(subject.name)
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ require 'factory_girl_rails'
10
+ require 'pry-rails'
11
+ require 'mongoid'
12
+ require 'mongoid_deep_cloneable'
13
+ Dir["#{File.dirname(__FILE__)}/factories/**/*.rb"].each {|f| require f}
14
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
15
+
16
+ RSpec.configure do |config|
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ config.run_all_when_everything_filtered = true
19
+ config.filter_run :focus
20
+ config.include FactoryGirl::Syntax::Methods
21
+ config.include MongoidDeepCloneable
22
+
23
+ # Run specs in random order to surface order dependencies. If you find an
24
+ # order dependency and want to debug it, you can fix the order by providing
25
+ # the seed, which is printed after each run.
26
+ # --seed 1234
27
+ config.order = 'random'
28
+ end
@@ -0,0 +1,61 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure.connect_to("mongoid_deep_cloneable")
4
+
5
+ class School
6
+ include Mongoid::Document
7
+
8
+ field :name, type: String
9
+
10
+ has_many :students
11
+ end
12
+
13
+ class Student
14
+ include Mongoid::Document
15
+
16
+ field :name, type: String
17
+ field :age, type: Integer
18
+
19
+ belongs_to :school
20
+
21
+ has_many :books
22
+ has_many :courses
23
+ has_one :wand
24
+ end
25
+
26
+ class Book
27
+ include Mongoid::Document
28
+
29
+ field :title, type: String
30
+ field :published_on, type: Date
31
+
32
+ has_many :spells
33
+
34
+ belongs_to :student
35
+ end
36
+
37
+ class Spell
38
+ include Mongoid::Document
39
+
40
+ field :name, type: String
41
+
42
+ belongs_to :book
43
+ end
44
+
45
+ class Course
46
+ include Mongoid::Document
47
+
48
+ field :name, type: String
49
+
50
+ belongs_to :student
51
+ end
52
+
53
+ class Wand
54
+ include Mongoid::Document
55
+
56
+ field :color, type: String
57
+ field :wood, type: String
58
+ field :inches, type: Float
59
+
60
+ belongs_to :student
61
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_deep_cloneable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1pre
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Macey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl_rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mongoid
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 4.0.0.alpha1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 4.0.0.alpha1
97
+ description: A gem used to clone a Mongoid object with its associations
98
+ email:
99
+ - shout@kylemacey.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/mongoid_deep_cloneable.rb
111
+ - lib/mongoid_deep_cloneable/version.rb
112
+ - mongoid_deep_cloneable.gemspec
113
+ - spec/factories/books.rb
114
+ - spec/factories/courses.rb
115
+ - spec/factories/schools.rb
116
+ - spec/factories/spells.rb
117
+ - spec/factories/students.rb
118
+ - spec/factories/wands.rb
119
+ - spec/mongoid_deep_cloneable/cloning_spec.rb
120
+ - spec/spec_helper.rb
121
+ - spec/support/mongoid.rb
122
+ homepage: ''
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>'
138
+ - !ruby/object:Gem::Version
139
+ version: 1.3.1
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.1.11
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: A gem used to clone a Mongoid object with its associations
146
+ test_files:
147
+ - spec/factories/books.rb
148
+ - spec/factories/courses.rb
149
+ - spec/factories/schools.rb
150
+ - spec/factories/spells.rb
151
+ - spec/factories/students.rb
152
+ - spec/factories/wands.rb
153
+ - spec/mongoid_deep_cloneable/cloning_spec.rb
154
+ - spec/spec_helper.rb
155
+ - spec/support/mongoid.rb