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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98b652c949f27c7bddcd372807affcdd75d17493
4
- data.tar.gz: ea175e6c84e532cb9e7a0efd837b6160ea5b8f15
3
+ metadata.gz: 12316fe1f1d8b76b86dfe2eccf71ed8a88e75cd3
4
+ data.tar.gz: 14b7c50140d8b81e921efaba7505cb3b8e6cee17
5
5
  SHA512:
6
- metadata.gz: 7bc1f3cc4511bf351b9bbe414cc29b82789fde75770f786e7f046960d384be222629d0fa9caaf5e24afb23f735fbd71fd14def11274af80de6ee7671fca9f93f
7
- data.tar.gz: 60019de6cab75f7c638938e2b030a49bc3136fe193f34369f4fbf60a6fbf2c5329c5307659cbcd9892d02b30393f0e1d76a4054b37b35d5ac1f316765e15b221
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, value)
24
- self.reject {|hash| hash[key] != value }
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 #scope method' do
25
- context 'symbol keys' do
26
- let(:sartre) { { name: 'Jean-Paul Sartre', nationality: 'French' } }
27
- let(:russell) { { name: 'Bertrand Russell', nationality: 'English' } }
28
- let(:wittgenstein) { { name: 'Ludwig Wittgenstein', nationality: 'Austrian' } }
29
- let(:camus) { { name: 'Albert Camus', nationality: 'French' } }
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
- it 'returns scoped hashes' do
33
- expect(array.scope(:nationality, 'French')).to eql([sartre, camus])
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
- context 'string keys' do
38
- let(:sartre) { {'name' => 'Jean-Paul Sartre', 'nationality' => 'French' } }
39
- let(:russell) { { 'name' => 'Bertrand Russell', 'nationality' => 'English' } }
40
- let(:wittgenstein) { { 'name' => 'Ludwig Wittgenstein', 'nationality' => 'Austrian' } }
41
- let(:camus) { { 'name' => 'Albert Camus', 'nationality' => 'French' } }
42
- let(:array) { [sartre, russell, wittgenstein, camus] }
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 'returns scoped hashes' do
45
- expect(array.scope('nationality', 'French')).to eql([sartre, camus])
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
@@ -5,8 +5,8 @@ module ReactiveSupport
5
5
 
6
6
  module Version
7
7
  MAJOR = '0'
8
- MINOR = '1'
9
- PATCH = '3'
8
+ MINOR = '2'
9
+ PATCH = '0'
10
10
  PRE = ''
11
11
 
12
12
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.').chomp('.')
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.1.3
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"