deep_clone 0.0.1
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.
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +23 -0
- data/README.md +31 -0
- data/Rakefile +32 -0
- data/deep_clone.gemspec +19 -0
- data/lib/deep_clone.rb +4 -0
- data/lib/deep_clone/extensions.rb +4 -0
- data/lib/deep_clone/extensions/array.rb +20 -0
- data/lib/deep_clone/extensions/hash.rb +23 -0
- data/lib/deep_clone/extensions/object.rb +18 -0
- data/lib/deep_clone/version.rb +3 -0
- data/spec/deep_clone_spec.rb +7 -0
- data/spec/extensions/array_spec.rb +21 -0
- data/spec/extensions/hash_spec.rb +21 -0
- data/spec/extensions/object_spec.rb +8 -0
- data/spec/spec_helper.rb +4 -0
- metadata +74 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2013 Sergey Gaychuk
|
2
|
+
Copyright (c) 2011-2013 Durran Jordan
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# DeepClone
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'deep_clone'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install deep_clone
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'deep_clone'
|
22
|
+
|
23
|
+
{a: 1, b: 2, c: {d: 3}, e: [100, 101, 102]}.__deep_clone__
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require "rake"
|
5
|
+
require "rspec"
|
6
|
+
require "rspec/core/rake_task"
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
9
|
+
require "deep_clone/version"
|
10
|
+
|
11
|
+
task :gem => :build
|
12
|
+
task :build do
|
13
|
+
system "gem build deep_clone.gemspec"
|
14
|
+
end
|
15
|
+
|
16
|
+
task :install => :build do
|
17
|
+
system "gem install deep_clone-#{DeepClone::VERSION}.gem"
|
18
|
+
end
|
19
|
+
|
20
|
+
task :release => :build do
|
21
|
+
system "git tag -a v#{DeepClone::VERSION} -m 'Tagging #{DeepClone::VERSION}'"
|
22
|
+
system "git push --tags"
|
23
|
+
system "gem push deep_clone-#{DeepClone::VERSION}.gem"
|
24
|
+
system "rm deep_clone-#{DeepClone::VERSION}.gem"
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec::Core::RakeTask.new("spec") do |spec|
|
28
|
+
spec.pattern = "spec/**/*_spec.rb"
|
29
|
+
spec.rspec_opts = %w(--format documentation --colour)
|
30
|
+
end
|
31
|
+
|
32
|
+
task :default => :spec
|
data/deep_clone.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'deep_clone/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "deep_clone"
|
8
|
+
gem.version = DeepClone::VERSION
|
9
|
+
gem.authors = ["Sergey Gaychuk"]
|
10
|
+
gem.email = ["sergey.gaychuk@gmail.com"]
|
11
|
+
gem.description = %q{Extends base classes for deep copy}
|
12
|
+
gem.summary = %q{Extends base classes for deep copy}
|
13
|
+
gem.homepage = "https://github.com/sergeygaychuk/deep_clone"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/deep_clone.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
module DeepClone
|
3
|
+
module Extensions
|
4
|
+
module Array
|
5
|
+
|
6
|
+
# Makes a deep copy of the array, deep copying every element inside the
|
7
|
+
# array.
|
8
|
+
#
|
9
|
+
# @example Get a deep copy of the array.
|
10
|
+
# [ 1, 2, 3 ].__deep_clone__
|
11
|
+
#
|
12
|
+
# @return [ Array ] The deep copy of the array.
|
13
|
+
def __deep_clone__
|
14
|
+
map { |value| value.__deep_clone__ }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
::Array.__send__(:include, DeepClone::Extensions::Array)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
module DeepClone
|
3
|
+
module Extensions
|
4
|
+
module Hash
|
5
|
+
|
6
|
+
# Make a deep copy of this hash.
|
7
|
+
#
|
8
|
+
# @example Make a deep copy of the hash.
|
9
|
+
# { field: value }.__deep_clone__
|
10
|
+
#
|
11
|
+
# @return [ Hash ] The copied hash.
|
12
|
+
def __deep_clone__
|
13
|
+
{}.tap do |copy|
|
14
|
+
each_pair do |key, value|
|
15
|
+
copy.store(key, value.__deep_clone__)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
::Hash.__send__(:include, DeepClone::Extensions::Hash)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module DeepClone
|
3
|
+
module Extensions
|
4
|
+
module Object
|
5
|
+
|
6
|
+
# Deep copy the object. This is for API compatibility, but needs to be
|
7
|
+
# overridden.
|
8
|
+
#
|
9
|
+
# @example Deep copy the object.
|
10
|
+
# 1.__deep_clone__
|
11
|
+
#
|
12
|
+
# @return [ Object ] self.
|
13
|
+
def __deep_clone__; self; end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
::Object.__send__(:include, DeepClone::Extensions::Object)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Hash do
|
5
|
+
let(:inner) { [3, 4] }
|
6
|
+
let(:data) { [ 1, 2, inner ] }
|
7
|
+
let(:copy) { data.__deep_clone__ }
|
8
|
+
|
9
|
+
it "should return an equal hash" do
|
10
|
+
data.should eq(copy)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return a copy" do
|
14
|
+
data.should_not equal(copy)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return an equal hash" do
|
18
|
+
copy[2].should eq(inner)
|
19
|
+
copy[2].should_not equal(inner)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Hash do
|
5
|
+
let(:inner) { { a: 3, b: 4 } }
|
6
|
+
let(:data) { { a: 1, b: 2, c: inner } }
|
7
|
+
let(:copy) { data.__deep_clone__ }
|
8
|
+
|
9
|
+
it "should return an equal hash" do
|
10
|
+
data.should eq(copy)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return a copy" do
|
14
|
+
data.should_not equal(copy)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return an equal hash" do
|
18
|
+
copy[:c].should eq(inner)
|
19
|
+
copy[:c].should_not equal(inner)
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deep_clone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sergey Gaychuk
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Extends base classes for deep copy
|
15
|
+
email:
|
16
|
+
- sergey.gaychuk@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- deep_clone.gemspec
|
28
|
+
- lib/deep_clone.rb
|
29
|
+
- lib/deep_clone/extensions.rb
|
30
|
+
- lib/deep_clone/extensions/array.rb
|
31
|
+
- lib/deep_clone/extensions/hash.rb
|
32
|
+
- lib/deep_clone/extensions/object.rb
|
33
|
+
- lib/deep_clone/version.rb
|
34
|
+
- spec/deep_clone_spec.rb
|
35
|
+
- spec/extensions/array_spec.rb
|
36
|
+
- spec/extensions/hash_spec.rb
|
37
|
+
- spec/extensions/object_spec.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
homepage: https://github.com/sergeygaychuk/deep_clone
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
hash: -3049193081357269342
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
hash: -3049193081357269342
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Extends base classes for deep copy
|
69
|
+
test_files:
|
70
|
+
- spec/deep_clone_spec.rb
|
71
|
+
- spec/extensions/array_spec.rb
|
72
|
+
- spec/extensions/hash_spec.rb
|
73
|
+
- spec/extensions/object_spec.rb
|
74
|
+
- spec/spec_helper.rb
|