ohm 0.0.24 → 0.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ohm.rb +30 -2
- data/test/model_test.rb +27 -0
- data/test/search_test.rb +42 -0
- metadata +3 -2
data/lib/ohm.rb
CHANGED
@@ -180,6 +180,10 @@ module Ohm
|
|
180
180
|
def size
|
181
181
|
db.llen(key)
|
182
182
|
end
|
183
|
+
|
184
|
+
def inspect
|
185
|
+
"#<List: #{raw.inspect}>"
|
186
|
+
end
|
183
187
|
end
|
184
188
|
|
185
189
|
# Represents a Redis set.
|
@@ -243,6 +247,7 @@ module Ohm
|
|
243
247
|
# end
|
244
248
|
# end
|
245
249
|
def filter(hash, &block)
|
250
|
+
raise ArgumentError, "filter expects a block" unless block_given?
|
246
251
|
apply(:sinterstore, keys(hash).push(key), &block)
|
247
252
|
end
|
248
253
|
|
@@ -255,6 +260,7 @@ module Ohm
|
|
255
260
|
# events = search_results.all
|
256
261
|
# end
|
257
262
|
def search(hash, &block)
|
263
|
+
raise ArgumentError, "search expects a block" unless block_given?
|
258
264
|
apply(:sunionstore, keys(hash), &block)
|
259
265
|
end
|
260
266
|
|
@@ -265,11 +271,15 @@ module Ohm
|
|
265
271
|
# Apply a redis operation on a collection of sets. Note that
|
266
272
|
# the resulting set is removed inmediatly after use.
|
267
273
|
def apply(operation, source, &block)
|
268
|
-
target = source.join(
|
274
|
+
target = source.uniq.join("+")
|
269
275
|
db.send(operation, target, *source)
|
270
276
|
set = self.class.new(db, target, model)
|
271
277
|
block.call(set)
|
272
|
-
set.delete!
|
278
|
+
set.delete! if source.size > 1
|
279
|
+
end
|
280
|
+
|
281
|
+
def inspect
|
282
|
+
"#<Set: #{raw.inspect}>"
|
273
283
|
end
|
274
284
|
|
275
285
|
private
|
@@ -397,6 +407,10 @@ module Ohm
|
|
397
407
|
new(:id => id) if exists?(id)
|
398
408
|
end
|
399
409
|
|
410
|
+
def self.to_proc
|
411
|
+
Proc.new { |id| self[id] }
|
412
|
+
end
|
413
|
+
|
400
414
|
def self.all
|
401
415
|
@all ||= Attributes::Set.new(db, key(:all), self)
|
402
416
|
end
|
@@ -557,6 +571,20 @@ module Ohm
|
|
557
571
|
self
|
558
572
|
end
|
559
573
|
|
574
|
+
def inspect
|
575
|
+
everything = (attributes + collections + counters).map do |att|
|
576
|
+
value = begin
|
577
|
+
send(att)
|
578
|
+
rescue ModelIsNew
|
579
|
+
nil
|
580
|
+
end
|
581
|
+
|
582
|
+
[att, value.inspect]
|
583
|
+
end
|
584
|
+
|
585
|
+
"#<#{self.class}:#{id || "?"} #{everything.map {|e| e.join("=") }.join(" ")}>"
|
586
|
+
end
|
587
|
+
|
560
588
|
protected
|
561
589
|
|
562
590
|
def key(*args)
|
data/test/model_test.rb
CHANGED
@@ -112,6 +112,10 @@ class TestRedis < Test::Unit::TestCase
|
|
112
112
|
assert_equal 1, User[1].id
|
113
113
|
assert_equal "albert@example.com", User[1].email
|
114
114
|
end
|
115
|
+
|
116
|
+
should "allow to map ids to models" do
|
117
|
+
assert_equal [User[1]], [1].map(&User)
|
118
|
+
end
|
115
119
|
end
|
116
120
|
|
117
121
|
context "Updating a user" do
|
@@ -526,4 +530,27 @@ class TestRedis < Test::Unit::TestCase
|
|
526
530
|
assert_not_equal @user, OpenStruct.new(:key => @user.send(:key))
|
527
531
|
end
|
528
532
|
end
|
533
|
+
|
534
|
+
context "Debugging" do
|
535
|
+
class ::Bar < Ohm::Model
|
536
|
+
attribute :name
|
537
|
+
counter :visits
|
538
|
+
set :friends
|
539
|
+
list :comments
|
540
|
+
end
|
541
|
+
|
542
|
+
should "provide a meaningful inspect" do
|
543
|
+
bar = Bar.new
|
544
|
+
|
545
|
+
assert_equal "#<Bar:? name=nil friends=nil comments=nil visits=0>", bar.inspect
|
546
|
+
|
547
|
+
bar.update(:name => "Albert")
|
548
|
+
bar.friends << 1
|
549
|
+
bar.friends << 2
|
550
|
+
bar.comments << "A"
|
551
|
+
bar.incr(:visits)
|
552
|
+
|
553
|
+
assert_equal %Q{#<Bar:#{bar.id} name="Albert" friends=#<Set: ["1", "2"]> comments=#<List: ["A"]> visits=1>}, Bar[bar.id].inspect
|
554
|
+
end
|
555
|
+
end
|
529
556
|
end
|
data/test/search_test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class SearchTest < Test::Unit::TestCase
|
4
|
+
setup do
|
5
|
+
Ohm.flush
|
6
|
+
end
|
7
|
+
|
8
|
+
class Lane < Ohm::Model
|
9
|
+
attribute :lane_type
|
10
|
+
|
11
|
+
index :lane_type
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
lane_type.capitalize
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate
|
18
|
+
assert_unique :lane_type
|
19
|
+
end
|
20
|
+
|
21
|
+
def error_messages
|
22
|
+
errors.present do |e|
|
23
|
+
e.on [:lane_type, :not_unique], "The lane type #{lane_type} is already in use"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "A model with an indexed attribute" do
|
29
|
+
setup do
|
30
|
+
@results = []
|
31
|
+
@subresults = []
|
32
|
+
Lane.search(:lane_type => "email") { |sr| @results << sr.size }
|
33
|
+
Lane.create(:lane_type => "email")
|
34
|
+
Lane.search(:lane_type => "email") { |sr| @results << sr.size }
|
35
|
+
Lane.search(:lane_type => "email") { |sr| @results << sr.size }
|
36
|
+
end
|
37
|
+
|
38
|
+
should "be able to find by the given attribute" do
|
39
|
+
assert_equal [0, 1, 1], @results
|
40
|
+
end
|
41
|
+
end
|
42
|
+
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.
|
4
|
+
version: 0.0.25
|
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-
|
13
|
+
date: 2009-10-07 00:00:00 -03:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- test/model_test.rb
|
41
41
|
- test/mutex_test.rb
|
42
42
|
- test/redis_test.rb
|
43
|
+
- test/search_test.rb
|
43
44
|
- test/test_helper.rb
|
44
45
|
- test/validations_test.rb
|
45
46
|
- test/test.conf
|