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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e09df24ba555d687f405c140c6ccbacaf4b42963
4
- data.tar.gz: 462f77bf9be2d36d7cd98f770c59b65149fdd5e8
3
+ metadata.gz: b6f4cc0d74155f2d9f36ba022fd256b28891cbff
4
+ data.tar.gz: 63ab2ed73f4460fbb655b09992bf85fa698bea2c
5
5
  SHA512:
6
- metadata.gz: 827ae9142869655c018017fe3ca335333561dc9504ab0ef46b5e0dae02b21b3b8f9c83bdad00090a6e9fc2c1ca9fc4fa289795cc1cdd5465a12680b07d037efb
7
- data.tar.gz: 3c92d873c50ddbb71f8ec8a67190edc65f003a67221526ceae430ee6446fb3a3bc6a3714ee2d5b76c07448265af1862e99668eb638b8fabd3990f719d7f38ec5
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
- In addition to the query logic, you can merge the arrays returned by `find_*`:
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.find_all(...) | store.find_any(...) | store.find_none(...)
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
- Note that the pipe operator `|` already eliminates duplicates:
149
+ Combine results using the union operator `|`:
136
150
 
137
151
  ```ruby
138
- [a, b, c] | [c, d, e]
139
- # => [a, b, c, d, e]
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 Strings will result in a performance penalty because `Object#send` expects Symbols.*
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 Symbol or String, it will be treated as a method name.
204
- *Note that providing a String will result in a performance penalty because `Object#send` expects Symbols.*
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 String:
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
  ```
@@ -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
@@ -1,3 +1,3 @@
1
1
  class MemStore
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.2"
3
3
  end
@@ -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.1
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: 2013-12-18 00:00:00.000000000 Z
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.0
55
+ rubygems_version: 2.2.2
56
56
  signing_key:
57
57
  specification_version: 4
58
58
  summary: A simple in-memory data store.