memstore 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +70 -10
- data/lib/memstore/core.rb +18 -0
- data/lib/memstore/version.rb +1 -1
- data/spec/core_methods_spec.rb +8 -0
- 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: b6f4cc0d74155f2d9f36ba022fd256b28891cbff
|
4
|
+
data.tar.gz: 63ab2ed73f4460fbb655b09992bf85fa698bea2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b1d6e1a2338a3d633d0966411daa316c46359c3f064fb64d47393a3262b4f98c1bd8b4682aff70a5df3ea568583bd33249d1ce6e1b2d2a3ea87585a3ce1c6f0
|
7
|
+
data.tar.gz: b2ae3c5fdb102a6e24e1b97f41ca6dcb0dc66b8ff84b159132f1a9a9e44e0db2f39b0b899dfcc58bde5918d0b02e825a121ec89650face919303a28441f8fcfc
|
data/README.md
CHANGED
@@ -83,6 +83,8 @@ For convenience, there are aliases for the `*_all` variants:
|
|
83
83
|
- `count` is an alias of `count_all`
|
84
84
|
- `delete` is an alias of `delete_all`
|
85
85
|
|
86
|
+
### Conditions
|
87
|
+
|
86
88
|
All methods take a hash of conditions and/or a block.
|
87
89
|
|
88
90
|
The hash is expected to map attributes (see [Customization](#customization)) to conditions.
|
@@ -119,6 +121,8 @@ refine Array do
|
|
119
121
|
end
|
120
122
|
```
|
121
123
|
|
124
|
+
### Block
|
125
|
+
|
122
126
|
The block is invoked with the item *after* the conditions are evaluated.
|
123
127
|
|
124
128
|
```ruby
|
@@ -126,19 +130,75 @@ store.find(age: 25) { |item| item.age - item.child.age > 20 }
|
|
126
130
|
# is equivalent to item.age == 25 && item.age - item.child.age > 20
|
127
131
|
```
|
128
132
|
|
129
|
-
|
133
|
+
### Operators
|
134
|
+
|
135
|
+
Since all queries return arrays, you can use set operations.
|
136
|
+
This can be especially useful to avoid overly complex queries.
|
137
|
+
|
138
|
+
Assume queries with these results:
|
130
139
|
|
131
140
|
```ruby
|
132
|
-
store.
|
141
|
+
store.find_any(...)
|
142
|
+
# => [a, b, c, d, e]
|
143
|
+
store.find_all(...)
|
144
|
+
# => [a, b, c]
|
145
|
+
store.find_none()
|
146
|
+
# => [b, c, e]
|
133
147
|
```
|
134
148
|
|
135
|
-
|
149
|
+
Combine results using the union operator `|`:
|
136
150
|
|
137
151
|
```ruby
|
138
|
-
|
139
|
-
# => [a, b, c,
|
152
|
+
store.find_all(...) | store.find_none(...)
|
153
|
+
# => [a, b, c, e]
|
154
|
+
```
|
155
|
+
|
156
|
+
Restrict results using the intersection operator `&`:
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
store.find_any(...) & store.find_none(...)
|
160
|
+
# => [a, d]
|
140
161
|
```
|
141
162
|
|
163
|
+
Note that both operators exclude duplicates and preserve order.
|
164
|
+
|
165
|
+
## Map & Reduce
|
166
|
+
|
167
|
+
MemStore provides a `map` method which is a shortcut to `store.all.map`:
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
store.map { |item| "#{item.name} (#{item.age})" }
|
171
|
+
# => ["Peter (23)", "Paul (42)", "Mary (33)"]
|
172
|
+
```
|
173
|
+
|
174
|
+
Since the result is an array, you can directly call `reduce` on it:
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
store.map { |item| item.name.length }.reduce { |sum, n| sum + n }
|
178
|
+
# => 13
|
179
|
+
```
|
180
|
+
|
181
|
+
If you simply want to grab a certain attribute from each item, you can use `collect` instead:
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
store.collect(:age)
|
185
|
+
# is equivalent to
|
186
|
+
store.map { |item| item.age }
|
187
|
+
```
|
188
|
+
|
189
|
+
This automatically uses the correct access method for attributes (see [Customization](#customization)).
|
190
|
+
|
191
|
+
It also returns an array so you can directly chain `reduce`:
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
store.collect(:age)
|
195
|
+
# => [23, 42, 33]
|
196
|
+
store.collect(:age).reduce(:+)
|
197
|
+
# => 98
|
198
|
+
```
|
199
|
+
|
200
|
+
*Of course, you can also use `items` or `all` to directly work with a hash or array of items.*
|
201
|
+
|
142
202
|
## Customization
|
143
203
|
|
144
204
|
### Default Behavior
|
@@ -169,7 +229,7 @@ store.find("age" => 42, "name" => "John")
|
|
169
229
|
# calls item.age and item.name to retrieve attributes
|
170
230
|
```
|
171
231
|
|
172
|
-
*Note that using
|
232
|
+
*Note that using strings will result in a performance penalty because `Object#send` expects symbols.*
|
173
233
|
|
174
234
|
### Custom Key
|
175
235
|
|
@@ -200,14 +260,14 @@ store.find(age: 42, name: "John")
|
|
200
260
|
# calls item[:age] and item[:name] to retrieve attributes
|
201
261
|
```
|
202
262
|
|
203
|
-
If you provide a
|
204
|
-
*Note that providing a
|
263
|
+
If you provide a symbol or string, it will be treated as a method name.
|
264
|
+
*Note that providing a string will result in a performance penalty because `Object#send` expects symbols.*
|
205
265
|
|
206
266
|
To access an attribute, MemStore will call the according method on your item and pass the requested attribute to it.
|
207
267
|
This means `key` and attributes in the conditions hash must be whatever your method expects:
|
208
268
|
|
209
269
|
```ruby
|
210
|
-
# assuming that items have a method `get` that expects a
|
270
|
+
# assuming that items have a method `get` that expects a string:
|
211
271
|
store = MemStore.new(key: "id", access: :get)
|
212
272
|
store << item
|
213
273
|
# calls item.get("id") to retrieve key
|
@@ -260,7 +320,7 @@ end
|
|
260
320
|
# lambda:
|
261
321
|
store = MemStore.new(access: -> item, attribute { special_accessor(item, attribute) })
|
262
322
|
# Proc:
|
263
|
-
store = MemStore.new(access: Proc.new { |item| special_accessor(item, attribute) })
|
323
|
+
store = MemStore.new(access: Proc.new { |item, attribute| special_accessor(item, attribute) })
|
264
324
|
# Method:
|
265
325
|
store = MemStore.new(access: method(:special_accessor))
|
266
326
|
```
|
data/lib/memstore/core.rb
CHANGED
@@ -126,4 +126,22 @@ class MemStore
|
|
126
126
|
keys.collect { |key| @items.delete(key) }
|
127
127
|
end
|
128
128
|
|
129
|
+
# Collects values of given attribute from all items.
|
130
|
+
#
|
131
|
+
# attribute - name of attribute to be collected (symbol or string)
|
132
|
+
#
|
133
|
+
# Returns an array of attribute values of each item.
|
134
|
+
def collect(attribute)
|
135
|
+
all.collect { |item| access_attribute(item, attribute) }
|
136
|
+
end
|
137
|
+
|
138
|
+
# Maps given block to all items.
|
139
|
+
#
|
140
|
+
# block - block to invoke with each item
|
141
|
+
#
|
142
|
+
# Returns an array of results from each block invocation.
|
143
|
+
def map(&block)
|
144
|
+
all.map(&block)
|
145
|
+
end
|
146
|
+
|
129
147
|
end
|
data/lib/memstore/version.rb
CHANGED
data/spec/core_methods_spec.rb
CHANGED
@@ -64,4 +64,12 @@ describe MemStore do
|
|
64
64
|
@store.all.must_equal([0.0, 1.0, 2.0, 7.0, 8.0, 9.0])
|
65
65
|
end
|
66
66
|
|
67
|
+
it "collects attribute values using the specified access method" do
|
68
|
+
@store.collect(:to_i).must_equal(@store.all.map { |n| n.to_i })
|
69
|
+
end
|
70
|
+
|
71
|
+
it "provides a shortcut to invoke map on all items" do
|
72
|
+
@store.map { |n| n.to_i }.must_equal(@store.all.map { |n| n.to_i })
|
73
|
+
end
|
74
|
+
|
67
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastian Klepper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: MemStore is a simple in-memory data store that supports complex search
|
14
14
|
queries.
|
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
54
|
rubyforge_project:
|
55
|
-
rubygems_version: 2.2.
|
55
|
+
rubygems_version: 2.2.2
|
56
56
|
signing_key:
|
57
57
|
specification_version: 4
|
58
58
|
summary: A simple in-memory data store.
|