safe_clone 0.0.8 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +14 -3
- data/lib/safe_clone.rb +44 -10
- data/lib/safe_clone/version.rb +1 -1
- data/rakefile.rb +5 -0
- data/reek.txt +1 -0
- data/safe_clone.gemspec +3 -4
- data/test/safe_clone_tests.rb +1 -5
- metadata +15 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f5c988942602b1795ede67effed38ffcacc8ffc52c5fa5226708a954070735f7
|
4
|
+
data.tar.gz: 481ad67eb3df2e17ad886ea8c2c3a905440812dcbe11203b51e72d86f34ae12a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a39d2f0d51a49f6dfda438cd37bc1fac7b859f6813e726d990fbb1c45c74e96bee2eb6e1ee1d50ad7dca653fd35fe4b71543bdc48f353a14993ad6c7b2436e3b
|
7
|
+
data.tar.gz: 1c9fd8d4891c7d137ce21381a94a59bb7305a84e2ba6cacb1f68413afa50179e8b0aa710c7034bf64a5168323c50c151bbe2ff47d12beec0c5ca4841222e4676
|
data/README.md
CHANGED
@@ -6,9 +6,10 @@ The justification for this uncharacteristic strictness is not at all clear, but
|
|
6
6
|
it does mean that the clone operation must be applied with great care.
|
7
7
|
|
8
8
|
Unlike the standard clone method, the safe\_clone method does not throw an
|
9
|
-
exception when
|
10
|
-
simply return themselves. This is correct because those types of objects
|
11
|
-
not
|
9
|
+
exception when it sees un-clonable value objects like 42 or true. These values
|
10
|
+
simply return themselves. This is deemed correct because those types of objects
|
11
|
+
are immutable and do not need to be duped. Instead of raising an exception,
|
12
|
+
the code returns the immutable object instead.
|
12
13
|
|
13
14
|
On a note about performance, this gem does not just rescue the exceptions
|
14
15
|
normally generated by clone, it prevents them from occurring and wasting time
|
@@ -190,3 +191,13 @@ Overall: Shorter code _and_ faster. Winner, winner, chicken dinner!
|
|
190
191
|
Go to the GitHub repository and raise an issue calling attention to some
|
191
192
|
aspect that could use some TLC or a suggestion or an idea.
|
192
193
|
|
194
|
+
## License
|
195
|
+
|
196
|
+
The gem is available as open source under the terms of the
|
197
|
+
[MIT License](./LICENSE.txt).
|
198
|
+
|
199
|
+
## Code of Conduct
|
200
|
+
|
201
|
+
Everyone interacting in the fully_freeze project’s codebases, issue trackers,
|
202
|
+
chat rooms and mailing lists is expected to follow the
|
203
|
+
[code of conduct](./CODE_OF_CONDUCT.md).
|
data/lib/safe_clone.rb
CHANGED
@@ -1,19 +1,53 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
1
3
|
require_relative "safe_clone/version"
|
2
4
|
|
5
|
+
# The default safe clone is Ruby clone.
|
3
6
|
class Object
|
4
7
|
#By default, reference types use the clone method.
|
5
|
-
def safe_clone
|
8
|
+
def safe_clone
|
9
|
+
clone
|
10
|
+
end
|
6
11
|
end
|
7
12
|
|
8
|
-
#For value types, just
|
13
|
+
# For value types, safe clone just returns self.
|
9
14
|
module SafeClone
|
10
|
-
def safe_clone
|
15
|
+
def safe_clone
|
16
|
+
self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Numeric does safe clone.
|
21
|
+
class Numeric
|
22
|
+
include SafeClone
|
23
|
+
end
|
24
|
+
|
25
|
+
# NilClass does safe clone.
|
26
|
+
class NilClass
|
27
|
+
include SafeClone
|
28
|
+
end
|
29
|
+
|
30
|
+
# TrueClass does safe clone.
|
31
|
+
class TrueClass
|
32
|
+
include SafeClone
|
11
33
|
end
|
12
34
|
|
13
|
-
#
|
14
|
-
class
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
class
|
35
|
+
# FalseClass does safe clone.
|
36
|
+
class FalseClass
|
37
|
+
include SafeClone
|
38
|
+
end
|
39
|
+
|
40
|
+
# Symbols do safe clone.
|
41
|
+
class Symbol
|
42
|
+
include SafeClone
|
43
|
+
end
|
44
|
+
|
45
|
+
# Regular expressions do safe clone.
|
46
|
+
class Regexp
|
47
|
+
include SafeClone
|
48
|
+
end
|
49
|
+
|
50
|
+
# Threads do safe clone.
|
51
|
+
class Thread
|
52
|
+
include SafeClone
|
53
|
+
end
|
data/lib/safe_clone/version.rb
CHANGED
data/rakefile.rb
CHANGED
@@ -8,6 +8,11 @@ Rake::TestTask.new do |t|
|
|
8
8
|
t.verbose = false
|
9
9
|
end
|
10
10
|
|
11
|
+
desc "Run a scan for smelly code!"
|
12
|
+
task :reek do |t|
|
13
|
+
`reek --no-color lib > reek.txt`
|
14
|
+
end
|
15
|
+
|
11
16
|
desc "Fire up an IRB session with safe_clone preloaded."
|
12
17
|
task :console do
|
13
18
|
system "ruby irbt.rb local"
|
data/reek.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0 total warnings
|
data/safe_clone.gemspec
CHANGED
@@ -17,9 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_development_dependency "rake", "
|
21
|
-
spec.add_development_dependency "bundler", "
|
20
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
21
|
+
spec.add_development_dependency "bundler", ">= 2.1.0"
|
22
22
|
spec.add_development_dependency 'minitest', "~> 5.7"
|
23
|
-
spec.add_development_dependency '
|
24
|
-
|
23
|
+
spec.add_development_dependency 'reek', "~> 5.0.2"
|
25
24
|
end
|
data/test/safe_clone_tests.rb
CHANGED
@@ -3,14 +3,10 @@
|
|
3
3
|
require_relative '../lib/safe_clone'
|
4
4
|
gem 'minitest'
|
5
5
|
require 'minitest/autorun'
|
6
|
-
require 'minitest_visible'
|
7
6
|
|
8
7
|
#Test the monkey patches applied to the Object class.
|
9
8
|
class SafeCloneTester < Minitest::Test
|
10
9
|
|
11
|
-
#Track mini-test progress.
|
12
|
-
include MinitestVisible
|
13
|
-
|
14
10
|
def test_for_safe_value_cloning
|
15
11
|
assert_equal((6).safe_clone, 6)
|
16
12
|
assert_equal((6).safe_clone.object_id, (6).object_id)
|
@@ -24,7 +20,7 @@ class SafeCloneTester < Minitest::Test
|
|
24
20
|
assert_equal((false).safe_clone, false)
|
25
21
|
assert_equal((false).safe_clone.object_id, (false).object_id)
|
26
22
|
|
27
|
-
|
23
|
+
assert_nil((nil).safe_clone)
|
28
24
|
assert_equal((nil).safe_clone.object_id, (nil).object_id)
|
29
25
|
|
30
26
|
rex = /ABC/
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe_clone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Camilleri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 12.3.3
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 12.3.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.1.0
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.1.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,19 +53,19 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.7'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: reek
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 5.0.2
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 5.0.2
|
69
69
|
description: A safer version of the clone method that avoids unnecessary exceptions.
|
70
70
|
email:
|
71
71
|
- peter.c.camilleri@gmail.com
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- lib/safe_clone.rb
|
84
84
|
- lib/safe_clone/version.rb
|
85
85
|
- rakefile.rb
|
86
|
+
- reek.txt
|
86
87
|
- safe_clone.gemspec
|
87
88
|
- test/safe_clone_tests.rb
|
88
89
|
homepage: https://github.com/PeterCamilleri/safe_clone
|
@@ -104,11 +105,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
105
|
- !ruby/object:Gem::Version
|
105
106
|
version: '0'
|
106
107
|
requirements: []
|
107
|
-
|
108
|
-
rubygems_version: 2.2.3
|
108
|
+
rubygems_version: 3.2.17
|
109
109
|
signing_key:
|
110
110
|
specification_version: 4
|
111
111
|
summary: A safer version of the clone method.
|
112
112
|
test_files:
|
113
113
|
- test/safe_clone_tests.rb
|
114
|
-
has_rdoc:
|