ohm 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -159,8 +159,37 @@ Ohm maintains different sets of objects ids for quick lookups.
159
159
  For example, in the example above, the index on the name attribute will
160
160
  allow for searches like Event.find(:name, "some value").
161
161
 
162
- Note that the `find` method and the `assert_unique` validation need a
163
- corresponding index to exist.
162
+ Note that the `assert_unique` validation and the methods `find`, `search` and `filter` need a
163
+ corresponding index in order to work.
164
+
165
+ ### Finding
166
+
167
+ You can find a collection of records with the `find` method:
168
+
169
+ # This returns a collection of users with the username "Albert"
170
+ User.find(username: "Albert")
171
+
172
+ ### Searching and filtering
173
+
174
+ # This returns a collection of users with usernames "Albert" or "Benoit"
175
+ User.search(username: ["Albert", "Benoit"]) do |search_results|
176
+ @users = search_results.all
177
+ end
178
+
179
+ # This returns a collection of users with usernames "Albert" or "Benoit",
180
+ # but only those with the account enabled.
181
+ User.search(username: ["Albert", "Benoit"]) do |search_results|
182
+ search_results.filter(account: "enabled") do |filter_results|
183
+ @users = filter_results.all
184
+ end
185
+ end
186
+
187
+ Important: note that both `search` and `filter` yield the results
188
+ to a block. This is important because the set of results is stored
189
+ for the duration of the block (to allow for chaining of searches and
190
+ filters), but is deleted once the block ends. The `.all` is necessary
191
+ for retrieving the actual instances, as keeping a reference to a set
192
+ that is going to be deleted would only return empty sets.
164
193
 
165
194
  Validations
166
195
  -----------
@@ -237,12 +266,7 @@ Given the following example:
237
266
  If all the assertions fail, the following errors will be present:
238
267
 
239
268
  obj.errors
240
- # => [[:foo, :not_present], [:bar, :not_numeric], [:baz, :format], [[:qux], :not_unique]]
241
-
242
- Note that the error for assert_unique wraps the field in an array.
243
- The purpose for this is to standardize the format for both single and
244
- multicolumn indexes.
245
-
269
+ # => [[:foo, :not_present], [:bar, :not_numeric], [:baz, :format], [:qux, :not_unique]]
246
270
 
247
271
  Presenting errors
248
272
  -----------------
data/lib/ohm.rb CHANGED
@@ -229,8 +229,15 @@ module Ohm
229
229
  # @see Ohm::Model.filter
230
230
  # @yield [results] Results of the filtering. Beware that the set of results is deleted from Redis when the block ends.
231
231
  # @example
232
+ # Event.filter(public: true) do |filter_results|
233
+ # @events = filter_results.all
234
+ # end
235
+ #
236
+ # # You can also combine search and filter
232
237
  # Event.search(day: "2009-09-11") do |search_results|
233
- # events = search_results.all
238
+ # search_results.filter(public: true) do |filter_results|
239
+ # @events = filter_results.all
240
+ # end
234
241
  # end
235
242
  def filter(hash, &block)
236
243
  apply(:sinterstore, keys(hash).push(key), &block)
@@ -242,9 +249,7 @@ module Ohm
242
249
  # @yield [results] Results of the search. Beware that the set of results is deleted from Redis when the block ends.
243
250
  # @example
244
251
  # Event.search(day: "2009-09-11") do |search_results|
245
- # search_results.filter(public: true) do |filter_results|
246
- # events = filter_results.all
247
- # end
252
+ # events = search_results.all
248
253
  # end
249
254
  def search(hash, &block)
250
255
  apply(:sunionstore, keys(hash), &block)
@@ -257,7 +262,7 @@ module Ohm
257
262
  # Apply a redis operation on a collection of sets. Note that
258
263
  # the resulting set is removed inmediatly after use.
259
264
  def apply(operation, source, &block)
260
- target = source.join(operation)
265
+ target = source.join(operation.to_s)
261
266
  db.send(operation, target, *source)
262
267
  set = self.class.new(db, target, model)
263
268
  block.call(set)
@@ -536,7 +541,7 @@ module Ohm
536
541
  end
537
542
 
538
543
  def ==(other)
539
- other.key == key
544
+ other.kind_of?(self.class) && other.key == key
540
545
  rescue ModelIsNew
541
546
  false
542
547
  end
@@ -112,9 +112,9 @@ class IndicesTest < Test::Unit::TestCase
112
112
  end
113
113
 
114
114
  setup do
115
- @event1 = Event.create(timeline: 1)
116
- @event2 = Event.create(timeline: 1)
117
- @event3 = Event.create(timeline: 2)
115
+ @event1 = Event.create(:timeline => 1)
116
+ @event2 = Event.create(:timeline => 1)
117
+ @event3 = Event.create(:timeline => 2)
118
118
  @event1.days = [1, 2]
119
119
  @event2.days = [2, 3]
120
120
  @event3.days = [3, 4]
@@ -124,27 +124,27 @@ class IndicesTest < Test::Unit::TestCase
124
124
  end
125
125
 
126
126
  should "intersect multiple sets of results" do
127
- Event.filter(timeline: 1, days: [1, 2]) do |set|
127
+ Event.filter(:timeline => 1, :days => [1, 2]) do |set|
128
128
  assert_equal [@event1], set
129
129
  end
130
130
  end
131
131
 
132
132
  should "group multiple sets of results" do
133
- Event.search(days: [1, 2]) do |set|
133
+ Event.search(:days => [1, 2]) do |set|
134
134
  assert_equal [@event1, @event2], set
135
135
  end
136
136
  end
137
137
 
138
138
  should "combine intersections and unions" do
139
- Event.search(days: [1, 2, 3]) do |events|
140
- events.filter(timeline: 1) do |result|
139
+ Event.search(:days => [1, 2, 3]) do |events|
140
+ events.filter(:timeline => 1) do |result|
141
141
  assert_equal [@event1, @event2], result
142
142
  end
143
143
  end
144
144
  end
145
145
 
146
146
  should "work with strings that generate a new line when encoded" do
147
- user = User.create(email: "foo@bar", update: "CORRECTED - UPDATE 2-Suspected US missile strike kills 5 in Pakistan")
147
+ user = User.create(:email => "foo@bar", :update => "CORRECTED - UPDATE 2-Suspected US missile strike kills 5 in Pakistan")
148
148
  assert_equal [user], User.find(:update, "CORRECTED - UPDATE 2-Suspected US missile strike kills 5 in Pakistan")
149
149
  end
150
150
  end
@@ -1,4 +1,5 @@
1
1
  require File.join(File.dirname(__FILE__), "test_helper")
2
+ require "ostruct"
2
3
 
3
4
  class Post < Ohm::Model
4
5
  attribute :body
@@ -227,7 +228,7 @@ class TestRedis < Test::Unit::TestCase
227
228
  Person.create :name => "B"
228
229
  Person.create :name => "A"
229
230
 
230
- assert_equal "A", Person.all.sort_by(:name, get: "Person:*:name", order: "ALPHA").first
231
+ assert_equal "A", Person.all.sort_by(:name, :get => "Person:*:name", :order => "ALPHA").first
231
232
  end
232
233
  end
233
234
 
@@ -489,5 +490,13 @@ class TestRedis < Test::Unit::TestCase
489
490
  should "not be comparable to instances of other models" do
490
491
  assert_not_equal @user, Event.create(:name => "Ruby Tuesday")
491
492
  end
493
+
494
+ should "be comparable to non-models" do
495
+ assert_not_equal @user, 1
496
+ assert_not_equal @user, true
497
+
498
+ # Not equal although the other object responds to #key.
499
+ assert_not_equal @user, OpenStruct.new(:key => @user.send(:key))
500
+ end
492
501
  end
493
502
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-09-15 00:00:00 -03:00
13
+ date: 2009-09-16 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies: []
16
16