hash_extend 1.1.2 → 1.1.3
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/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/lib/hash_extend.rb +50 -11
- data/lib/hash_extend/version.rb +1 -1
- data/spec/lib/hash_extend_spec.rb +88 -0
- metadata +24 -40
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hash_extend (1.1.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rspec (2.11.0)
|
11
|
+
rspec-core (~> 2.11.0)
|
12
|
+
rspec-expectations (~> 2.11.0)
|
13
|
+
rspec-mocks (~> 2.11.0)
|
14
|
+
rspec-core (2.11.1)
|
15
|
+
rspec-expectations (2.11.3)
|
16
|
+
diff-lcs (~> 1.1.3)
|
17
|
+
rspec-mocks (2.11.3)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
hash_extend!
|
24
|
+
rspec
|
data/lib/hash_extend.rb
CHANGED
@@ -1,17 +1,50 @@
|
|
1
1
|
require "hash_extend/version"
|
2
2
|
|
3
3
|
class Hash
|
4
|
+
|
5
|
+
|
6
|
+
unless method_defined? :except!
|
7
|
+
|
8
|
+
def except! *keys
|
9
|
+
|
10
|
+
p "is personnal methods"
|
11
|
+
|
12
|
+
keys.each do |key|
|
13
|
+
delete key
|
14
|
+
end
|
15
|
+
return self
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
unless method_defined? :youpi
|
21
|
+
|
22
|
+
def youpi
|
23
|
+
|
24
|
+
p "youpi"
|
4
25
|
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
5
34
|
# delete key(s) but return self instead of deleted value
|
6
35
|
def stealth_delete! *keys
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
return self
|
36
|
+
|
37
|
+
except! keys
|
38
|
+
warn "WARNING : 'stealth_delete!' is deprecated and will be deleted in version X.Y - Use 'extract!' to fit Rails 3's ActiveSupport"
|
11
39
|
end
|
12
40
|
|
41
|
+
def stealth_delete *keys
|
42
|
+
|
43
|
+
except! keys
|
44
|
+
warn "WARNING : method 'stealth_delete' is deprecated and will be deleted in version X.Y - Use 'extract' to fit Rails 3's ActiveSupport"
|
45
|
+
end
|
13
46
|
|
14
|
-
# modify values
|
47
|
+
# modify values from hash through block
|
15
48
|
def map_values!
|
16
49
|
self.each do |key, value|
|
17
50
|
self[key] = yield value
|
@@ -33,7 +66,7 @@ class Hash
|
|
33
66
|
|
34
67
|
# delete severals keys in one time
|
35
68
|
def delete_many *keys
|
36
|
-
keys.map{ |key|
|
69
|
+
keys.map{ |key| delete(key) }
|
37
70
|
end
|
38
71
|
|
39
72
|
|
@@ -78,12 +111,18 @@ class Hash
|
|
78
111
|
|
79
112
|
|
80
113
|
# duplicate method without self modification
|
81
|
-
[:
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
114
|
+
[:except, :map_values, :compact, :select_by].each do |method_name|
|
115
|
+
|
116
|
+
unless method_defined? method_name
|
117
|
+
|
118
|
+
define_method method_name do |*args, &block|
|
119
|
+
hash = self.dup
|
120
|
+
eval "hash.#{ method_name }! *args, &block"
|
121
|
+
return hash
|
122
|
+
end
|
123
|
+
|
86
124
|
end
|
125
|
+
|
87
126
|
end
|
88
127
|
|
89
128
|
end
|
data/lib/hash_extend/version.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
require './lib/hash_extend'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
subject { { a: {b: 1}, b: 2 } }
|
5
|
+
let!(:old) { subject }
|
6
|
+
describe '#stealth_delete!(*keys)' do
|
7
|
+
it 'returns the pre-delete hash' do
|
8
|
+
subject.stealth_delete!(:b).should == subject
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'deletes a key' do
|
12
|
+
subject.stealth_delete!(:b)
|
13
|
+
subject.should_not have_key(:b)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#map_values!' do
|
18
|
+
it 'modifies values from hash through block' do
|
19
|
+
subject.map_values! { 1 }
|
20
|
+
subject.should == { a: 1, b: 1 }
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns modified hash' do
|
24
|
+
subject.map_values! { 2 }.should == { a: 2, b: 2 }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#map_keys(&block)' do
|
29
|
+
it 'modifies keys from hash through block' do
|
30
|
+
subject.map_keys { 2 }.should == { 2 => 2 }
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'does not modify the hash' do
|
34
|
+
subject.map_keys { 2 }
|
35
|
+
subject.should == old
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#map_keys!(&block)' do
|
40
|
+
it 'modifies the hash changing keys through block' do
|
41
|
+
subject.map_keys! { 2 }
|
42
|
+
subject.should == { 2 => 2 }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#delete_many(*keys)' do
|
47
|
+
it 'can delete one key' do
|
48
|
+
subject.delete_many(:a).should == [{b: 1}]
|
49
|
+
subject.should_not have_key(:a)
|
50
|
+
subject.should have_key(:b)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'can delete many keys' do
|
54
|
+
subject.delete_many(:a, :b).should == [{b: 1}, 2]
|
55
|
+
subject.should_not have_key(:a)
|
56
|
+
subject.should_not have_key(:b)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#insert(value, *keys)' do
|
61
|
+
it 'can add to the hash' do
|
62
|
+
subject.insert(1, :c)
|
63
|
+
subject[:c].should == 1
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'can modify the hash' do
|
67
|
+
subject.insert(15, :b)
|
68
|
+
subject[:b].should == 15
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'can insert to a nested hash' do
|
72
|
+
subject.insert(15, :c, :d)
|
73
|
+
subject[:c][:d].should == 15
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'returns itself' do
|
77
|
+
subject.insert(15, :c, :d).should == subject
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#compact!' do
|
82
|
+
it 'pending'
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#select_by!' do
|
86
|
+
it 'pending'
|
87
|
+
end
|
88
|
+
end
|
metadata
CHANGED
@@ -1,73 +1,57 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_extend
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.3
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
- 2
|
10
|
-
version: 1.1.2
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Thomas Petrachi
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2012-10-01 00:00:00 Z
|
12
|
+
date: 2012-12-01 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
14
|
description: Extend ruby Hash. No override.
|
22
|
-
email:
|
15
|
+
email:
|
23
16
|
- thomas.petrachi@vodeclic.com
|
24
17
|
executables: []
|
25
|
-
|
26
18
|
extensions: []
|
27
|
-
|
28
19
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
20
|
+
files:
|
31
21
|
- .gitignore
|
32
22
|
- Gemfile
|
23
|
+
- Gemfile.lock
|
33
24
|
- LICENSE.txt
|
34
25
|
- README.md
|
35
26
|
- Rakefile
|
36
27
|
- hash_extend.gemspec
|
37
28
|
- lib/hash_extend.rb
|
38
29
|
- lib/hash_extend/version.rb
|
30
|
+
- spec/lib/hash_extend_spec.rb
|
39
31
|
homepage: https://github.com/petrachi/hash_extend
|
40
32
|
licenses: []
|
41
|
-
|
42
33
|
post_install_message:
|
43
34
|
rdoc_options: []
|
44
|
-
|
45
|
-
require_paths:
|
35
|
+
require_paths:
|
46
36
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
38
|
none: false
|
49
|
-
requirements:
|
50
|
-
- -
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
|
53
|
-
|
54
|
-
- 0
|
55
|
-
version: "0"
|
56
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
44
|
none: false
|
58
|
-
requirements:
|
59
|
-
- -
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
segments:
|
63
|
-
- 0
|
64
|
-
version: "0"
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
65
49
|
requirements: []
|
66
|
-
|
67
50
|
rubyforge_project:
|
68
51
|
rubygems_version: 1.8.24
|
69
52
|
signing_key:
|
70
53
|
specification_version: 3
|
71
|
-
summary: Adding methods %w{stealth_delete map_values map_keys delete_many insert compact
|
72
|
-
|
73
|
-
|
54
|
+
summary: Adding methods %w{stealth_delete map_values map_keys delete_many insert compact
|
55
|
+
select_by}
|
56
|
+
test_files:
|
57
|
+
- spec/lib/hash_extend_spec.rb
|