ohm 0.1.0.rc4 → 0.1.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +7 -3
- data/lib/ohm/key.rb +24 -8
- data/lib/ohm/pattern.rb +37 -0
- data/lib/ohm/utils/upgrade.rb +2 -2
- data/lib/ohm/version.rb +1 -1
- data/lib/ohm.rb +183 -121
- data/test/connection_test.rb +1 -9
- data/test/indices_test.rb +3 -1
- data/test/model_test.rb +158 -98
- data/test/mutex_test.rb +4 -4
- data/test/pattern_test.rb +8 -0
- data/test/test_helper.rb +43 -2
- data/test/upgrade_script_test.rb +1 -1
- metadata +6 -7
- data/lib/ohm/collection.rb +0 -186
- data/test/model_module_test.rb +0 -951
data/lib/ohm/collection.rb
DELETED
@@ -1,186 +0,0 @@
|
|
1
|
-
module Ohm
|
2
|
-
class Collection
|
3
|
-
include Enumerable
|
4
|
-
|
5
|
-
attr_accessor :key, :db
|
6
|
-
|
7
|
-
def initialize(key, db = Ohm.redis)
|
8
|
-
self.key = key
|
9
|
-
self.db = db
|
10
|
-
end
|
11
|
-
|
12
|
-
def each(&block)
|
13
|
-
all.each(&block)
|
14
|
-
end
|
15
|
-
|
16
|
-
# Return the values as model instances, ordered by the options supplied.
|
17
|
-
# Check redis documentation to see what values you can provide to each option.
|
18
|
-
#
|
19
|
-
# @param options [Hash] options to sort the collection.
|
20
|
-
# @option options [#to_s] :by Model attribute to sort the instances by.
|
21
|
-
# @option options [#to_s] :order (ASC) Sorting order, which can be ASC or DESC.
|
22
|
-
# @option options [Integer] :limit (all) Number of items to return.
|
23
|
-
# @option options [Integer] :start (0) An offset from where the limit will be applied.
|
24
|
-
#
|
25
|
-
# @example Get the first ten users sorted alphabetically by name:
|
26
|
-
#
|
27
|
-
# @event.attendees.sort(:by => :name, :order => "ALPHA", :limit => 10)
|
28
|
-
#
|
29
|
-
# @example Get five posts sorted by number of votes and starting from the number 5 (zero based):
|
30
|
-
#
|
31
|
-
# @blog.posts.sort(:by => :votes, :start => 5, :limit => 10")
|
32
|
-
def sort(options = {})
|
33
|
-
return [] if empty?
|
34
|
-
options[:start] ||= 0
|
35
|
-
options[:limit] = [options[:start], options[:limit]] if options[:limit]
|
36
|
-
db.sort(key, options)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Sort the model instances by id and return the first instance
|
40
|
-
# found. If a :by option is provided with a valid attribute name, the
|
41
|
-
# method sort_by is used instead and the option provided is passed as the
|
42
|
-
# first parameter.
|
43
|
-
#
|
44
|
-
# @see #sort
|
45
|
-
# @return [Ohm::Model, nil] Returns the first instance found or nil.
|
46
|
-
def first(options = {})
|
47
|
-
options = options.merge(:limit => 1)
|
48
|
-
sort(options).first
|
49
|
-
end
|
50
|
-
|
51
|
-
def [](index)
|
52
|
-
first(:start => index)
|
53
|
-
end
|
54
|
-
|
55
|
-
def to_ary
|
56
|
-
all
|
57
|
-
end
|
58
|
-
|
59
|
-
def ==(other)
|
60
|
-
to_ary == other
|
61
|
-
end
|
62
|
-
|
63
|
-
# @return [true, false] Returns whether or not the collection is empty.
|
64
|
-
def empty?
|
65
|
-
size.zero?
|
66
|
-
end
|
67
|
-
|
68
|
-
# Clears the values in the collection.
|
69
|
-
def clear
|
70
|
-
db.del(key)
|
71
|
-
self
|
72
|
-
end
|
73
|
-
|
74
|
-
# Appends the given values to the collection.
|
75
|
-
def concat(values)
|
76
|
-
values.each { |value| self << value }
|
77
|
-
self
|
78
|
-
end
|
79
|
-
|
80
|
-
# Replaces the collection with the passed values.
|
81
|
-
def replace(values)
|
82
|
-
clear
|
83
|
-
concat(values)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
# Represents a Redis list.
|
88
|
-
#
|
89
|
-
# @example Use a list attribute.
|
90
|
-
#
|
91
|
-
# class Event < Ohm::Model
|
92
|
-
# attribute :name
|
93
|
-
# list :participants
|
94
|
-
# end
|
95
|
-
#
|
96
|
-
# event = Event.create :name => "Redis Meeting"
|
97
|
-
# event.participants << "Albert"
|
98
|
-
# event.participants << "Benoit"
|
99
|
-
# event.participants.all
|
100
|
-
# # => ["Albert", "Benoit"]
|
101
|
-
class List < Collection
|
102
|
-
|
103
|
-
# @param value [#to_s] Pushes value to the tail of the list.
|
104
|
-
def << value
|
105
|
-
db.rpush(key, value)
|
106
|
-
end
|
107
|
-
|
108
|
-
alias push <<
|
109
|
-
|
110
|
-
# @return [String] Return and remove the last element of the list.
|
111
|
-
def pop
|
112
|
-
db.rpop(key)
|
113
|
-
end
|
114
|
-
|
115
|
-
# @return [String] Return and remove the first element of the list.
|
116
|
-
def shift
|
117
|
-
db.lpop(key)
|
118
|
-
end
|
119
|
-
|
120
|
-
# @param value [#to_s] Pushes value to the head of the list.
|
121
|
-
def unshift(value)
|
122
|
-
db.lpush(key, value)
|
123
|
-
end
|
124
|
-
|
125
|
-
# @return [Array] Elements of the list.
|
126
|
-
def all
|
127
|
-
db.lrange(key, 0, -1)
|
128
|
-
end
|
129
|
-
|
130
|
-
# @return [Integer] Returns the number of elements in the list.
|
131
|
-
def size
|
132
|
-
db.llen(key)
|
133
|
-
end
|
134
|
-
|
135
|
-
def include?(value)
|
136
|
-
all.include?(value)
|
137
|
-
end
|
138
|
-
|
139
|
-
def inspect
|
140
|
-
"#<List: #{all.inspect}>"
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
# Represents a Redis set.
|
145
|
-
#
|
146
|
-
# @example Use a set attribute.
|
147
|
-
#
|
148
|
-
# class Company < Ohm::Model
|
149
|
-
# attribute :name
|
150
|
-
# set :employees
|
151
|
-
# end
|
152
|
-
#
|
153
|
-
# company = Company.create :name => "Redis Co."
|
154
|
-
# company.employees << "Albert"
|
155
|
-
# company.employees << "Benoit"
|
156
|
-
# company.employees.all #=> ["Albert", "Benoit"]
|
157
|
-
# company.employees.include?("Albert") #=> true
|
158
|
-
class Set < Collection
|
159
|
-
|
160
|
-
# @param value [#to_s] Adds value to the list.
|
161
|
-
def << value
|
162
|
-
db.sadd(key, value)
|
163
|
-
end
|
164
|
-
|
165
|
-
def delete(value)
|
166
|
-
db.srem(key, value)
|
167
|
-
end
|
168
|
-
|
169
|
-
def include?(value)
|
170
|
-
db.sismember(key, value)
|
171
|
-
end
|
172
|
-
|
173
|
-
def all
|
174
|
-
db.smembers(key)
|
175
|
-
end
|
176
|
-
|
177
|
-
# @return [Integer] Returns the number of elements in the set.
|
178
|
-
def size
|
179
|
-
db.scard(key)
|
180
|
-
end
|
181
|
-
|
182
|
-
def inspect
|
183
|
-
"#<Set: #{all.inspect}>"
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|