reactive_support 0.3.0.beta → 0.3.0.beta2
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.
- checksums.yaml +4 -4
- data/lib/reactive_support/extensions/hash_extensions.rb +47 -0
- data/reactive_support.gemspec +1 -1
- data/spec/reactive_extensions_spec.rb +78 -0
- data/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a54091a52002588f7f971f0d2327ff2d5f521675
|
4
|
+
data.tar.gz: 3fa7f6ba5efdaa4b4565d25013ff5e43179f6ae0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f693c85385f91a5bcd4d5d5eb652b7695c5cdf816c6fbc91c02ed7c05a307e429849e05002d7cdad510fd6d7e87a3e56a8693eeda141ee034342632562cccc0
|
7
|
+
data.tar.gz: a3b687c0248b3f58245ec69ae532d4a4ed0d16e382583336e833228ea0f151b1da9f5d8c6f66da2615be8084800286f5c592bf8e6260db43e4c4b42313458118
|
@@ -1,5 +1,52 @@
|
|
1
1
|
class Hash
|
2
2
|
|
3
|
+
# The +#clean+ method returns a hash identical to the calling hash but
|
4
|
+
# with the given +*keys+, if present, removed. If all the keys in the
|
5
|
+
# hash are given as arguments, an empty hash is returned. The +#clean+
|
6
|
+
# method is non-destructive; the original hash is still available after
|
7
|
+
# it is called. It does have a destructive form, +#clean!+.
|
8
|
+
#
|
9
|
+
# Examples:
|
10
|
+
# hash = {:foo => 'bar'}
|
11
|
+
# hash.clean(:baz) # => {:foo => 'bar'}
|
12
|
+
# hash.clean(:foo) # => {}
|
13
|
+
# hash # => {:foo => 'bar'}
|
14
|
+
#
|
15
|
+
# Examples with multiple arguments:
|
16
|
+
# hash = { 'foo' => 'bar', 'baz' => 'qux', => :norf => 'raboof' }
|
17
|
+
# hash.clean('foo', :norf) # => {'baz' => 'qux'}
|
18
|
+
# hash # => { 'foo' => 'bar', 'baz' => 'qux', :norf => 'raboof' }
|
19
|
+
|
20
|
+
def clean(*keys)
|
21
|
+
self.reject {|k,v| k.in?(keys) }
|
22
|
+
end
|
23
|
+
|
24
|
+
# The +#clean!+ method returns a hash identical to the calling hash but
|
25
|
+
# with the given +*keys+, if present, removed. If all the keys in the
|
26
|
+
# hash are given as arguments, an empty hash is returned. The +#clean!+
|
27
|
+
# method is non-destructive; the original hash is still available after
|
28
|
+
# it is called. It does have a non-destructive form, +#clean+.
|
29
|
+
#
|
30
|
+
# Examples where hash is not changed:
|
31
|
+
# hash = {:foo => 'bar'}
|
32
|
+
# hash.clean!(:baz) # => {:foo => 'bar'}
|
33
|
+
# hash # => {:foo => 'bar'}
|
34
|
+
#
|
35
|
+
# Examples where hash is changed:
|
36
|
+
# hash = {'foo' => 'bar'}
|
37
|
+
# hash.clean!(:foo) # => {}
|
38
|
+
# hash # => {}
|
39
|
+
#
|
40
|
+
# Examples with multiple arguments:
|
41
|
+
# hash = { 'foo' => 'bar', 'baz' => 'qux', => :norf => 'raboof' }
|
42
|
+
# hash.clean('foo', :norf) # => {'baz' => 'qux'}
|
43
|
+
# hash # => {'baz' => 'qux'}
|
44
|
+
|
45
|
+
def clean!(*keys)
|
46
|
+
self.reject! {|k,v| k.in?(keys) }
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
3
50
|
# The +#symbolize_keys+ method returns a hash identical to the calling
|
4
51
|
# hash but with string keys turned into symbols. It is non-destructive;
|
5
52
|
# the original hash is still available after it is called.
|
data/reactive_support.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
|
9
9
|
s.name = 'reactive_support'
|
10
10
|
s.version = ReactiveSupport.gem_version
|
11
|
-
s.date = '2014-11-
|
11
|
+
s.date = '2014-11-27'
|
12
12
|
|
13
13
|
s.description = "ActiveSupport methods re-implemented independently of the Rails ecosystem"
|
14
14
|
s.summary = "ReactiveSupport provides useful ActiveSupport methods in a gem that is simple, stable, agnostic, and transparent."
|
@@ -137,6 +137,84 @@ describe ReactiveExtensions do
|
|
137
137
|
expect { hash.stringify_keys! }.to change(hash, :keys)
|
138
138
|
end
|
139
139
|
end
|
140
|
+
|
141
|
+
describe 'clean' do
|
142
|
+
context 'when no cleaning is needed' do
|
143
|
+
it 'returns the original hash' do
|
144
|
+
expect({:foo => 'bar'}.clean(:baz)). to eql({:foo => 'bar'})
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'doesn\'t raise an error' do
|
148
|
+
expect{ {:foo => 'bar'}.clean(:baz) }.not_to raise_error
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'when there are some keys to clean' do
|
153
|
+
it 'removes specified keys' do
|
154
|
+
expect({:foo => 'bar', :baz => 'qux'}.clean(:baz)).to eql({:foo => 'bar'})
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'doesn\'t change the original hash' do
|
158
|
+
hash = {:foo => 'bar', :baz => 'qux', 'norf' => 'raboof'}
|
159
|
+
expect{ hash.clean(:baz, 'norf') }.not_to change(hash, :keys)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'when all keys must be cleaned' do
|
164
|
+
it 'returns an empty hash' do
|
165
|
+
expect({:foo => 'bar'}.clean(:foo)).to eql({})
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'doesn\'t change the original hash' do
|
169
|
+
hash = {:foo => 'bar'}
|
170
|
+
expect{ hash.clean(:foo) }.not_to change(hash, :keys)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe 'clean!' do
|
176
|
+
context 'when no cleaning is needed' do
|
177
|
+
it 'returns the original hash' do
|
178
|
+
expect({:foo => 'bar'}.clean!(:baz)). to eql({:foo => 'bar'})
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'doesn\'t raise an error' do
|
182
|
+
expect{ {:foo => 'bar'}.clean(:baz) }.not_to raise_error
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context 'when there are some keys to clean' do
|
187
|
+
it 'removes specified keys' do
|
188
|
+
expect({:foo => 'bar', :baz => 'qux'}.clean!(:baz)).to eql({:foo => 'bar'})
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'changes the original hash' do
|
192
|
+
hash = {:foo => 'bar', :baz => 'qux', 'norf' => 'raboof'}
|
193
|
+
expect{ hash.clean!(:baz, 'norf') }.to change(hash, :keys)
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'returns the hash' do
|
197
|
+
hash = {:foo => 'bar', :baz => 'qux'}
|
198
|
+
expect(hash.clean!(:baz)).to eql hash
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'when all keys must be cleaned' do
|
203
|
+
it 'returns an empty hash' do
|
204
|
+
expect({:foo => 'bar'}.clean!(:foo)).to eql({})
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'changes the original hash' do
|
208
|
+
hash = {:foo => 'bar'}
|
209
|
+
expect{ hash.clean!(:foo) }.to change(hash, :keys)
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'returns the hash' do
|
213
|
+
hash = {:foo => 'bar'}
|
214
|
+
expect(hash.clean!(:foo)).to eql hash
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
140
218
|
end
|
141
219
|
|
142
220
|
describe 'proc methods' do
|
data/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reactive_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.0.
|
4
|
+
version: 0.3.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dana Scheider
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
version: 1.3.1
|
124
124
|
requirements: []
|
125
125
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
126
|
+
rubygems_version: 2.2.2
|
127
127
|
signing_key:
|
128
128
|
specification_version: 1
|
129
129
|
summary: ReactiveSupport provides useful ActiveSupport methods in a gem that is simple,
|