safe_clone 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +9 -0
- data/lib/safe_clone/version.rb +3 -0
- data/lib/safe_clone.rb +38 -0
- data/safe_clone.gemspec +25 -0
- data/test/safe_clone_tests.rb +58 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NTdlNGE5MTBlOTMyNTkxY2IwNzIxZjFiZTc0M2ZmYTIwYzAyNzVhMQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MzJmZGRjOWQwZTRjMTUzMGIyNGEyODVkNzE1OWIwZGEzNzRkY2U2NA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmU3MDkyYjIyMTc3YTRkMjEyMDIyYTY5NzIzZWU0ZjYyZjE1Njc0ODQwYmNk
|
10
|
+
NGNkODQyZGU2ZjcwNmM1Nzg3YTAzNjkyNzY3MTE3ODIzNjZmOWViMDUwMDE5
|
11
|
+
OTI3MGQ2MDY4ODVhNTAwMTM5OTg5MDk5OWQ4OTczOWM5MmM0Zjc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YTlmMzBiNGRlZGJjN2E4ZDJiZjU1YTZjMzQ1OTdmMDY5ZTY0ZWE5MjY3MWFj
|
14
|
+
MTk2M2QwYTYyMDUwM2UzMDViZTg1M2QwYWI4MTk5MjQyZGQxMmFiNmY3MGRi
|
15
|
+
YTQxYzMxNzYyY2FlYWU1MWMxZjI2NjZkNjMxMDVkMWQ2Yzc0MTI=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Peter Camilleri
|
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,42 @@
|
|
1
|
+
# SafeClone
|
2
|
+
|
3
|
+
This tiny gem implements a version of clone called safe_clone. Unlike the
|
4
|
+
standard clone method, the safe_clone method does not throw ant exception
|
5
|
+
when sent to un-clonable objects like 42 or true. These simply return
|
6
|
+
themselves and this is correct because those types of objects do not NEED
|
7
|
+
to be cloned. Instead of blowing up in a hissy fit, the code just works!
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'safe_clone'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install safe_clone
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
require 'safe_clone'
|
26
|
+
|
27
|
+
then, in those places where regular clone was problematic, use:
|
28
|
+
|
29
|
+
foo = my_object.safe_clone
|
30
|
+
|
31
|
+
instead of
|
32
|
+
|
33
|
+
foo = my_object.clone
|
34
|
+
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/safe_clone.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "safe_clone/version"
|
2
|
+
|
3
|
+
module SafeClone
|
4
|
+
def safe_clone
|
5
|
+
self
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Object
|
10
|
+
def safe_clone
|
11
|
+
self.clone
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Numeric
|
16
|
+
include SafeClone
|
17
|
+
end
|
18
|
+
|
19
|
+
class NilClass
|
20
|
+
include SafeClone
|
21
|
+
end
|
22
|
+
|
23
|
+
class TrueClass
|
24
|
+
include SafeClone
|
25
|
+
end
|
26
|
+
|
27
|
+
class FalseClass
|
28
|
+
include SafeClone
|
29
|
+
end
|
30
|
+
|
31
|
+
class Symbol
|
32
|
+
include SafeClone
|
33
|
+
end
|
34
|
+
|
35
|
+
class Regexp
|
36
|
+
include SafeClone
|
37
|
+
end
|
38
|
+
|
data/safe_clone.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'safe_clone/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "safe_clone"
|
8
|
+
spec.version = SafeClone::VERSION
|
9
|
+
spec.authors = ["Peter Camilleri"]
|
10
|
+
spec.email = ["peter.c.camilleri@gmail.com"]
|
11
|
+
spec.description = "A safer version of the clone method that avoids unnecessary exceptions."
|
12
|
+
spec.summary = "A safer version of the clone method."
|
13
|
+
spec.homepage = "http://teuthida-technologies.com/"
|
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 = spec.files.grep(%r{^(test|spec|features)/})
|
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 'minitest'
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../lib/safe_clone'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
#Test the monkey patches applied to the Object class.
|
7
|
+
class SafeCloneTester < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
#Special initialize to track rake progress.
|
10
|
+
def initialize(*all)
|
11
|
+
$do_this_only_one_time = "" unless defined? $do_this_only_one_time
|
12
|
+
|
13
|
+
if $do_this_only_one_time != __FILE__
|
14
|
+
puts
|
15
|
+
puts "Running test file: #{File.split(__FILE__)[1]}"
|
16
|
+
$do_this_only_one_time = __FILE__
|
17
|
+
end
|
18
|
+
|
19
|
+
super(*all)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_for_safe_value_cloning
|
23
|
+
assert_equal((6).safe_clone, 6)
|
24
|
+
assert_equal((6).safe_clone.object_id, (6).object_id)
|
25
|
+
|
26
|
+
assert_equal((:foo).safe_clone, :foo)
|
27
|
+
assert_equal((:foo).safe_clone.object_id, (:foo).object_id)
|
28
|
+
|
29
|
+
assert_equal((true).safe_clone, true)
|
30
|
+
assert_equal((true).safe_clone.object_id, (true).object_id)
|
31
|
+
|
32
|
+
assert_equal((false).safe_clone, false)
|
33
|
+
assert_equal((false).safe_clone.object_id, (false).object_id)
|
34
|
+
|
35
|
+
assert_equal((nil).safe_clone, nil)
|
36
|
+
assert_equal((nil).safe_clone.object_id, (nil).object_id)
|
37
|
+
|
38
|
+
rex = /ABC/
|
39
|
+
assert_equal(rex.safe_clone, rex)
|
40
|
+
assert_equal(rex.safe_clone.object_id, rex.object_id)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_for_reference_cloning
|
44
|
+
str = "test"
|
45
|
+
assert_equal(str.safe_clone, str)
|
46
|
+
refute_equal(str.safe_clone.object_id, str.object_id)
|
47
|
+
|
48
|
+
arra = [1,2,3]
|
49
|
+
arrb = arra.safe_clone
|
50
|
+
assert_equal(arrb, arra)
|
51
|
+
refute_equal(arrb.object_id, arra.object_id)
|
52
|
+
|
53
|
+
arrb << 4
|
54
|
+
assert_equal([1,2,3], arra)
|
55
|
+
assert_equal([1,2,3,4], arrb)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: safe_clone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Camilleri
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-02 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: minitest
|
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
|
+
description: A safer version of the clone method that avoids unnecessary exceptions.
|
56
|
+
email:
|
57
|
+
- peter.c.camilleri@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/safe_clone.rb
|
68
|
+
- lib/safe_clone/version.rb
|
69
|
+
- safe_clone.gemspec
|
70
|
+
- test/safe_clone_tests.rb
|
71
|
+
homepage: http://teuthida-technologies.com/
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.1.4
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: A safer version of the clone method.
|
95
|
+
test_files:
|
96
|
+
- test/safe_clone_tests.rb
|
97
|
+
has_rdoc:
|