redis-objects 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +45 -13
- data/lib/redis/list.rb +20 -3
- data/lib/redis/set.rb +1 -0
- data/spec/redis_objects_model_spec.rb +7 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= Redis::Objects -
|
1
|
+
= Redis::Objects - Map Redis types directly to Ruby objects
|
2
2
|
|
3
3
|
This is *not* an ORM. People that are wrapping ORM's around Redis are missing
|
4
4
|
the point.
|
@@ -53,22 +53,21 @@ Familiar Ruby array operations Just Work (TM):
|
|
53
53
|
@team.on_base << 'player1'
|
54
54
|
@team.on_base << 'player2'
|
55
55
|
@team.on_base << 'player3'
|
56
|
-
|
56
|
+
@team.on_base # ['player1', 'player2']
|
57
57
|
@team.on_base.pop
|
58
58
|
@team.on_base.shift
|
59
59
|
@team.on_base.length # 1
|
60
60
|
@team.on_base.delete('player3')
|
61
|
-
|
61
|
+
|
62
62
|
Sets work like the Ruby {Set}[http://ruby-doc.org/core/classes/Set.html] class:
|
63
63
|
|
64
|
-
@team.outfielders
|
65
|
-
@team.outfielders
|
66
|
-
@team.outfielders['
|
67
|
-
@team.outfielders.
|
68
|
-
|
69
|
-
puts "#{player} is playing #{position}"
|
64
|
+
@team.outfielders << 'outfielder1' << 'outfielder1'
|
65
|
+
@team.outfielders << 'outfielder2'
|
66
|
+
@team.outfielders # ['outfielder1', 'outfielder2']
|
67
|
+
@team.outfielders.each do |player|
|
68
|
+
puts player
|
70
69
|
end
|
71
|
-
|
70
|
+
player = @team.outfielders.detect{|of| of == 'outfielder2'}
|
72
71
|
|
73
72
|
Note counters cannot be assigned to, only incremented/decremented:
|
74
73
|
|
@@ -101,15 +100,48 @@ Or can pass the handle into the new method:
|
|
101
100
|
@counter = Redis::Counter.new('counter_name')
|
102
101
|
@counter.increment
|
103
102
|
@counter.decrement
|
104
|
-
puts @counter
|
105
|
-
|
103
|
+
puts @counter.value
|
104
|
+
@counter.increment do |val|
|
105
|
+
raise "Full" if val > MAX_VAL # rewind counter
|
106
|
+
end
|
107
|
+
|
108
|
+
See the section on "Atomicity" for cool uses of atomic counter blocks.
|
106
109
|
|
107
110
|
=== Lists
|
108
111
|
|
112
|
+
These work just like Ruby arrays:
|
113
|
+
|
109
114
|
@list = Redis::List.new('list_name')
|
110
115
|
@list << 'a'
|
111
116
|
@list << 'b'
|
112
|
-
|
117
|
+
@list.include? 'c' # false
|
118
|
+
@list.values # ['a','b']
|
119
|
+
@list.shift
|
120
|
+
@list.pop
|
121
|
+
@list.clear
|
122
|
+
# etc
|
123
|
+
|
124
|
+
=== Sets
|
125
|
+
|
126
|
+
Sets work like the Ruby {Set}[http://ruby-doc.org/core/classes/Set.html] class:
|
127
|
+
|
128
|
+
@set = Redis::Set.new('set_name')
|
129
|
+
@set << 'a'
|
130
|
+
@set << 'b'
|
131
|
+
@set << 'a'
|
132
|
+
@set.member? 'c' # false
|
133
|
+
@set.members # ['a','b']
|
134
|
+
@set.each do |member|
|
135
|
+
puts member
|
136
|
+
end
|
137
|
+
@set.clear
|
138
|
+
# etc
|
139
|
+
|
140
|
+
=== Values
|
141
|
+
|
142
|
+
@value = Redis::Value.new('value_name')
|
143
|
+
@value.value = 'a'
|
144
|
+
@value.delete
|
113
145
|
|
114
146
|
== Atomicity
|
115
147
|
|
data/lib/redis/list.rb
CHANGED
@@ -59,37 +59,54 @@ class Redis
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
# Delete the element(s) from the list that match name. If count is specified,
|
63
|
+
# only the first-N (if positive) or last-N (if negative) will be removed.
|
64
|
+
# Redis: LREM
|
62
65
|
def delete(name, count=0)
|
63
66
|
redis.lrem(key, count, name) # weird api
|
64
67
|
get
|
65
68
|
end
|
66
69
|
|
70
|
+
# Iterate through each member of the set. Redis::Objects mixes in Enumerable,
|
71
|
+
# so you can also use familiar methods like +collect+, +detect+, and so forth.
|
67
72
|
def each(&block)
|
68
73
|
values.each(&block)
|
69
74
|
end
|
70
75
|
|
76
|
+
# Return a range of values from +start_index+ to +end_index+. Can also use
|
77
|
+
# the familiar list[start,end] Ruby syntax. Redis: LRANGE
|
71
78
|
def range(start_index, end_index)
|
72
79
|
redis.lrange(key, start_index, end_index)
|
73
80
|
end
|
74
81
|
|
75
|
-
# Return the value at the given index.
|
82
|
+
# Return the value at the given index. Can also use familiar list[index] syntax.
|
83
|
+
# Redis: LINDEX
|
76
84
|
def at(index)
|
77
85
|
redis.lindex(key, index)
|
78
86
|
end
|
79
87
|
|
88
|
+
# Return the first element in the list. Redis: LINDEX(0)
|
89
|
+
def first
|
90
|
+
at(0)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Return the last element in the list. Redis: LINDEX(-1)
|
80
94
|
def last
|
81
|
-
|
95
|
+
at(-1)
|
82
96
|
end
|
83
97
|
|
98
|
+
# Clear the list entirely. Redis: DEL
|
84
99
|
def clear
|
85
100
|
redis.del(key)
|
86
101
|
end
|
87
102
|
|
103
|
+
# Return the length of the list. Aliased as size. Redis: LLEN
|
88
104
|
def length
|
89
105
|
redis.llen(key)
|
90
106
|
end
|
91
107
|
alias_method :size, :length
|
92
|
-
|
108
|
+
|
109
|
+
# Returns true if there are no elements in the list. Redis: LLEN == 0
|
93
110
|
def empty?
|
94
111
|
length == 0
|
95
112
|
end
|
data/lib/redis/set.rb
CHANGED
@@ -264,12 +264,16 @@ describe Redis::Objects do
|
|
264
264
|
@roster.player_stats.push 'c'
|
265
265
|
@roster.player_stats.should == ['b','a','c']
|
266
266
|
@roster.player_stats.get.should == ['b','a','c']
|
267
|
+
@roster.player_stats.first.should == 'b'
|
268
|
+
@roster.player_stats.last.should == 'c'
|
267
269
|
@roster.player_stats << 'd'
|
268
270
|
@roster.player_stats.should == ['b','a','c','d']
|
269
271
|
@roster.player_stats[1].should == 'a'
|
270
272
|
@roster.player_stats[0].should == 'b'
|
271
273
|
@roster.player_stats[2].should == 'c'
|
272
274
|
@roster.player_stats[3].should == 'd'
|
275
|
+
@roster.player_stats.include?('c').should be_true
|
276
|
+
@roster.player_stats.include?('no').should be_false
|
273
277
|
@roster.player_stats.pop
|
274
278
|
@roster.player_stats[0].should == @roster.player_stats.at(0)
|
275
279
|
@roster.player_stats[1].should == @roster.player_stats.at(1)
|
@@ -350,7 +354,9 @@ describe Redis::Objects do
|
|
350
354
|
@roster.outfielders.get.should == ['a','b']
|
351
355
|
|
352
356
|
@roster.outfielders << 'c'
|
353
|
-
@roster.outfielders.member?
|
357
|
+
@roster.outfielders.member?('c').should be_true
|
358
|
+
@roster.outfielders.include?('c').should be_true
|
359
|
+
@roster.outfielders.member?('no').should be_false
|
354
360
|
coll = @roster.outfielders.select{|st| st == 'c'}
|
355
361
|
coll.should == ['c']
|
356
362
|
@roster.outfielders.sort.should == ['a','b','c']
|