reactive_support 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/extensions/array_extensions.rb +28 -3
- data/lib/extensions/hash_extensions.rb +88 -0
- data/spec/reactive_extensions_spec.rb +109 -17
- data/version.rb +2 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12316fe1f1d8b76b86dfe2eccf71ed8a88e75cd3
|
4
|
+
data.tar.gz: 14b7c50140d8b81e921efaba7505cb3b8e6cee17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6c2208404d0eec5f24732fc965833f62181cc41bfd8853d3c21a6d6b91b65e508ce120f77aa7c90562090d2ac3dff29388a78bd3427521766b042c5e3004e9b
|
7
|
+
data.tar.gz: 2d3057e90220183049bda134a4fd412628649697e442a8e1ea8fa03ea45d222373176d4d70fe3568eb9d265a434f5acbd7926927c3be7eb1a09d4e784938c50c
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class Array
|
2
2
|
|
3
3
|
# The +#scope+ method is called on an array of hashes. It returns a sub-array
|
4
|
-
# including only hashes for which the given +key+ equals the given +value+.
|
4
|
+
# including only hashes for which the value at a given +key+ equals the given +value+.
|
5
5
|
# The +#scope+ method is non-destructive; the original array will remain intact
|
6
6
|
# after it is called. The +#scope+ method is known to work for string or symbol
|
7
7
|
# keys. It should work for other data type keys as well.
|
@@ -20,7 +20,32 @@ class Array
|
|
20
20
|
# { name: 'Albert Camus', nationality: 'French' }
|
21
21
|
# ]
|
22
22
|
|
23
|
-
def scope(key,
|
24
|
-
self.
|
23
|
+
def scope(key, *values)
|
24
|
+
self.select {|hash| hash[key].in?(values) }
|
25
|
+
end
|
26
|
+
|
27
|
+
# The +#where_not+ method is called on an array of hashes. It returns a sub-array
|
28
|
+
# including only hashes for which the value at a given +key+ does not equal the
|
29
|
+
# given value. It is the inverse of the +#scope+ method. The +#where_not+ method
|
30
|
+
# is non-destructive; the original array will remain intact after it is called. The
|
31
|
+
# +#where_not+ method is known to work for string or symbol keys. It should work for
|
32
|
+
# other data types as well.
|
33
|
+
#
|
34
|
+
# Example:
|
35
|
+
# array = [
|
36
|
+
# { name: 'Jean-Paul Sartre', nationality: 'French' },
|
37
|
+
# { name: 'Bertrand Russell', nationality: 'English' },
|
38
|
+
# { name: 'Ludwig Wittgenstein', nationality: 'Austrian' },
|
39
|
+
# { name: 'Albert Camus', nationality: 'French' }
|
40
|
+
# ]
|
41
|
+
#
|
42
|
+
# array.where_not(:nationality, 'French')
|
43
|
+
# # => [
|
44
|
+
# { name: 'Bertrand Russell', nationality: 'English' },
|
45
|
+
# { name: 'Ludwig Wittgenstein', nationality: 'English' }
|
46
|
+
# ]
|
47
|
+
|
48
|
+
def where_not(key, *values)
|
49
|
+
self.reject {|hash| hash[key].in?(values) }
|
25
50
|
end
|
26
51
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# The +#symbolize_keys+ method returns a hash identical to the calling
|
4
|
+
# hash but with string keys turned into symbols. It is non-destructive;
|
5
|
+
# the original hash is still available after it is called.
|
6
|
+
#
|
7
|
+
# Although this method was formerly a part of ActiveSupport, it was
|
8
|
+
# already deprecated by the time ReactiveSupport was introduced. For
|
9
|
+
# that reason, it is being included as part of ReactiveExtensions.
|
10
|
+
#
|
11
|
+
# Examples:
|
12
|
+
# orig = { 'foo' => 'bar' }
|
13
|
+
# dup = orig.symbolize_keys
|
14
|
+
#
|
15
|
+
# orig #=> { 'foo' => 'bar' }
|
16
|
+
# dup #=> { :foo => 'bar' }
|
17
|
+
|
18
|
+
def symbolize_keys
|
19
|
+
dup = {}
|
20
|
+
self.each {|k, v| dup[k.to_sym] = v }
|
21
|
+
dup
|
22
|
+
end
|
23
|
+
|
24
|
+
# The +#symbolize_keys!+ method converts string hash keys into symbols.
|
25
|
+
# It is a destructive method; the original hash is changed when this
|
26
|
+
# method is called.
|
27
|
+
#
|
28
|
+
# Although this method was formerly a part of ActiveSupport, it was already
|
29
|
+
# deprecated by the time ReactiveSupport was introduced. For that reason,
|
30
|
+
# it is being included as part of ReactiveExtensions.
|
31
|
+
#
|
32
|
+
# Examples:
|
33
|
+
# orig = { 'foo' => 'bar' }
|
34
|
+
# orig.symbolize_keys!
|
35
|
+
#
|
36
|
+
# orig #=> { :foo => 'bar' }
|
37
|
+
|
38
|
+
def symbolize_keys!
|
39
|
+
keys.each do |key|
|
40
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
41
|
+
end
|
42
|
+
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
# The +#stringify_keys+ method returns a hash identical to the calling
|
47
|
+
# hash, but with symbol keys turned into strings. It is non-destructive;
|
48
|
+
# the original hash is still available after it is called.
|
49
|
+
#
|
50
|
+
# Although this method was formerly a part of ActiveSupport, it was
|
51
|
+
# already deprected by the time ReactiveSupport was introduced. For
|
52
|
+
# that reason, it is being included as part of ReactiveExtensions.
|
53
|
+
#
|
54
|
+
# Examples:
|
55
|
+
# orig = { :foo => 'bar' }
|
56
|
+
# dup = orig.stringify_keys
|
57
|
+
#
|
58
|
+
# orig #=> { :foo => 'bar' }
|
59
|
+
# dup #=> { 'foo' => 'bar' }
|
60
|
+
|
61
|
+
def stringify_keys
|
62
|
+
dup = {}
|
63
|
+
self.each {|k, v| dup[k.to_s] = v }
|
64
|
+
dup
|
65
|
+
end
|
66
|
+
|
67
|
+
# The +#stringify_keys!+ method converts symbol hash keys into strings.
|
68
|
+
# It is a destructive method; the original hash is changed when this
|
69
|
+
# method is called.
|
70
|
+
#
|
71
|
+
# Although this method was formerly a part of ActiveSupport, it was already
|
72
|
+
# deprecated by the time ReactiveSupport was introduced. For that reason,
|
73
|
+
# it is being included as part of ReactiveExtensions.
|
74
|
+
#
|
75
|
+
# Examples:
|
76
|
+
# orig = { :foo => 'bar' }
|
77
|
+
# orig.symbolize_keys!
|
78
|
+
#
|
79
|
+
# orig #=> { 'foo' => 'bar' }
|
80
|
+
|
81
|
+
def stringify_keys!
|
82
|
+
keys.each do |key|
|
83
|
+
self[(key.to_s rescue key) || key] = delete(key)
|
84
|
+
end
|
85
|
+
|
86
|
+
self
|
87
|
+
end
|
88
|
+
end
|
@@ -21,28 +21,120 @@ describe ReactiveExtensions do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
describe 'array
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
let(:array) { [sartre, russell, wittgenstein, camus] }
|
24
|
+
describe 'array methods' do
|
25
|
+
let(:sartre) { { 'name' => 'Jean-Paul Sartre', 'nationality' => 'French' } }
|
26
|
+
let(:russell) { { 'name' => 'Bertrand Russell', 'nationality' => 'English' } }
|
27
|
+
let(:wittgenstein) { { 'name' => 'Ludwig Wittgenstein', 'nationality' => 'Austrian' } }
|
28
|
+
let(:camus) { { 'name' => 'Albert Camus', 'nationality' => 'French' } }
|
29
|
+
let(:array) { [sartre, russell, wittgenstein, camus] }
|
31
30
|
|
32
|
-
|
33
|
-
|
31
|
+
describe 'array #scope method' do
|
32
|
+
context 'symbol keys' do
|
33
|
+
context 'single value' do
|
34
|
+
it 'returns scoped hashes' do
|
35
|
+
array.each {|hash| hash.symbolize_keys! }
|
36
|
+
expect(array.scope(:nationality, 'French')).to eql([sartre, camus])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'multiple values' do
|
41
|
+
it 'returns scoped hashes' do
|
42
|
+
array.each {|hash| hash.symbolize_keys! }
|
43
|
+
expect(array.scope(:nationality, 'French', 'English')).to eql([sartre, russell, camus])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'string keys' do
|
49
|
+
context 'single value' do
|
50
|
+
it 'returns scoped hashes' do
|
51
|
+
expect(array.scope('nationality', 'French')).to eql([sartre, camus])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'multiple values' do
|
56
|
+
it 'returns scoped hashes' do
|
57
|
+
expect(array.scope('nationality', 'French', 'English')).to eql([sartre, russell, camus])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'array #where_not method' do
|
64
|
+
context 'symbol keys' do
|
65
|
+
context 'single value' do
|
66
|
+
it 'returns scoped hashes' do
|
67
|
+
array.each {|hash| hash.symbolize_keys! }
|
68
|
+
expect(array.where_not(:nationality, 'French')).to eql([russell, wittgenstein])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'multiple values' do
|
73
|
+
it 'returns scoped hashes' do
|
74
|
+
array.each {|hash| hash.symbolize_keys! }
|
75
|
+
expect(array.where_not(:nationality, 'French', 'English')).to eql([wittgenstein])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'string keys' do
|
81
|
+
context 'single value' do
|
82
|
+
it 'returns scoped hashes' do
|
83
|
+
expect(array.where_not('nationality', 'French')).to eql([russell, wittgenstein])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'multiple values' do
|
88
|
+
it 'returns scoped hashes' do
|
89
|
+
expect(array.where_not('nationality', 'French', 'English')).to eql([wittgenstein])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe 'hash methods' do
|
97
|
+
describe 'symbolize_keys' do
|
98
|
+
it 'turns string keys into symbols' do
|
99
|
+
expect({'foo' => 'bar'}.symbolize_keys).to eql({:foo => 'bar'})
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'maintains the original hash' do
|
103
|
+
hash = { 'foo' => 'bar' }
|
104
|
+
expect { hash.symbolize_keys }.not_to change(hash, :keys)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe 'symbolize_keys!' do
|
109
|
+
it 'turns string keys into symbols' do
|
110
|
+
expect({'foo' => 'bar'}.symbolize_keys!).to eql({:foo => 'bar'})
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'overwrites the original hash' do
|
114
|
+
hash = { 'foo' => 'bar' }
|
115
|
+
expect { hash.symbolize_keys! }.to change(hash, :keys)
|
34
116
|
end
|
35
117
|
end
|
36
118
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
119
|
+
describe 'stringify_keys' do
|
120
|
+
it 'turns symbol keys into strings' do
|
121
|
+
expect({foo: 'bar'}.stringify_keys).to eql({'foo' => 'bar'})
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'maintains the original hash' do
|
125
|
+
hash = { foo: 'bar' }
|
126
|
+
expect { hash.stringify_keys }.not_to change(hash, :keys)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe 'stringify_keys!' do
|
131
|
+
it 'turns symbol keys into strings' do
|
132
|
+
expect({foo: 'bar'}.stringify_keys!).to eql({'foo' => 'bar'})
|
133
|
+
end
|
43
134
|
|
44
|
-
it '
|
45
|
-
|
135
|
+
it 'overwrites the original hash' do
|
136
|
+
hash = { foo: 'bar' }
|
137
|
+
expect { hash.stringify_keys! }.to change(hash, :keys)
|
46
138
|
end
|
47
139
|
end
|
48
140
|
end
|
data/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reactive_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dana Scheider
|
@@ -76,6 +76,7 @@ extra_rdoc_files:
|
|
76
76
|
- LICENSE
|
77
77
|
files:
|
78
78
|
- "./lib/extensions/array_extensions.rb"
|
79
|
+
- "./lib/extensions/hash_extensions.rb"
|
79
80
|
- "./lib/extensions/reactive_extensions.rb"
|
80
81
|
- "./lib/reactive_support.rb"
|
81
82
|
- "./lib/reactive_support/core_ext/array/access.rb"
|