blombo 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGELOG +2 -0
  2. data/README +19 -12
  3. data/blombo.gemspec +2 -2
  4. data/lib/blombo.rb +97 -16
  5. data/test/test_all.rb +33 -6
  6. metadata +4 -4
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.4. Redis list commands
2
+
1
3
  v0.3. Enable multi-threading
2
4
 
3
5
  v0.2. Couple of utility new methods
data/README CHANGED
@@ -1,4 +1,4 @@
1
- Blombo
1
+ kBlombo
2
2
  -------
3
3
 
4
4
  Treat redis-server like a deep ruby hash.
@@ -47,23 +47,30 @@ range of ruby hash methods:
47
47
 
48
48
  Blombo only saves the object back into redis if the []= method is used.
49
49
  Assignment must always be used to save to db. This means you should
50
- maintain ruby objects like arrays carefully:
50
+ maintain ruby objects carefully:
51
+
52
+ $blombo[:servers] = {:name => 'web1'}
53
+ $blombo[:servers].merge!(:ip => '10.0.0.1')
54
+ $blombo[:servers] # => {:name=>"web1"} # oops
55
+
56
+
57
+ Redis List types
58
+ ----------------
59
+
60
+ Treat blombo like an array and it'll marshal data to a redis list type.
61
+
62
+ $blombo.servers = [1,2]
63
+ $blombo.servers << 3
64
+ $blombo.servers.shift # => 1
65
+ $blombo.servers.last # => 3
51
66
 
52
- $blombo.servers[:list] = ["web1"]
53
- $blombo.servers[:list] << "web2"
54
- $blombo.servers[:list]
55
- #=> ["web1"] #oops
56
67
 
57
- $blombo.servers[:list] += ["web2"]
58
- $blombo.servers[:list]
59
- #=> ["web1", "web2"] #ahh!
60
-
61
68
 
62
69
  Other Redis types
63
70
  -----------------
64
71
 
65
- What if I don't want a Redis hash? Thats OK - Blombo passes through redis
66
- commands curried with the key as the first parameter:
72
+ What if I need a specific redis command? Thats OK - Blombo passes through
73
+ redis commands curried with the key as the first parameter:
67
74
 
68
75
  This: $blombo.joblist.rpush('job1')
69
76
  Equals: $redis.rpush('blombo:ServerApp:joblist', 'job1')
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "blombo"
5
- s.version = "0.3"
5
+ s.version = "0.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andrew Snow"]
9
- s.date = "2012-02-19"
9
+ s.date = "2012-04-05"
10
10
  s.description = "Blombo: Treat redis-server like a deep ruby hash"
11
11
  s.email = "andrew@modulus.org"
12
12
  s.extra_rdoc_files = ["CHANGELOG", "README", "lib/blombo.rb"]
@@ -76,20 +76,11 @@ class Blombo
76
76
  @name
77
77
  end
78
78
 
79
- def []=(key, val)
80
- redis.hset(blombo_key, key.to_s, self.class.to_redis_val(val))
81
- end
82
79
 
83
80
  def defined?(key)
84
81
  redis.exists(key.to_s)
85
82
  end
86
83
 
87
- def [](key)
88
- if(val = redis.hget(blombo_key, key.to_s))
89
- self.class.from_redis_val(val)
90
- end
91
- end
92
-
93
84
  def nil?
94
85
  empty?
95
86
  end
@@ -99,23 +90,31 @@ class Blombo
99
90
  end
100
91
 
101
92
  def to_hash
102
- redis.hgetall(blombo_key)
93
+ keys.inject({}) {|h,k| h.merge!(k => self[k]) }
103
94
  end
104
95
 
105
96
  def each(*args, &bl)
106
- to_hash.each(*args, &bl)
97
+ if(type == 'list')
98
+ lrange(0, -1).map {|va| each(*args, &bl) }
99
+ else
100
+ to_hash.each(*args, &bl)
101
+ end
107
102
  end
108
103
 
109
104
  def to_a
110
- redis.hgetall(blombo_key).to_a
105
+ if(type == 'list')
106
+ lrange(0, -1).map {|val| self.class.from_redis_val(val) }
107
+ else
108
+ to_hash.to_a
109
+ end
111
110
  end
112
111
 
113
112
  def keys
114
- self.class.redis.hkeys(blombo_key)
113
+ hkeys
115
114
  end
116
115
 
117
116
  def values
118
- self.class.redis.hvals(blombo_key).map {|v| self.class.from_redis_val(v) }
117
+ hvals.map {|v| self.class.from_redis_val(v) }
119
118
  end
120
119
 
121
120
  def clear
@@ -130,14 +129,96 @@ class Blombo
130
129
  if Blombo.redis.respond_to?(meth)
131
130
  Blombo.redis.send(meth, blombo_key, *params, &bl)
132
131
  elsif params.empty? && meth =~ /^[a-z_][a-z0-9_]*$/i
133
- Blombo.new("#{@name}:#{meth}", self)
132
+ key(meth)
133
+ elsif params.length == 1 && meth =~ /^(.*)=$/
134
+ if Hash === params[0]
135
+ key($1).del
136
+ params[0].each {|k,val| key($1)[k] = val }
137
+ elsif Enumerable === params[0]
138
+ key($1).del
139
+ params[0].each {|val| key($1).push(val) }
140
+ else
141
+ raise TypeError.new('Blombo setters must be sent a Hash or Array')
142
+ end
134
143
  else
135
144
  super(meth, *params, &bl)
136
145
  end
137
146
  end
147
+
148
+ def key(*args)
149
+ Blombo.new("#{@name}:#{args.join(':')}", self)
150
+ end
138
151
 
139
152
  def blombo_children
140
- self.class.redis.keys("#{blombo_key}:*").map {|k| Blombo.new(k.gsub(/^blombo:/,''), self) }
153
+ blombo_children_names.map {|k| Blombo.new(k, self) }
154
+ end
155
+
156
+ def blombo_children_names
157
+ self.class.redis.keys("#{blombo_key}:*").map {|k| k.gsub(/^blombo:/,'') }
158
+ end
159
+
160
+
161
+ def <<(x)
162
+ push(x)
163
+ end
164
+
165
+ def push(*x)
166
+ rpush(*(x.map {|val| self.class.to_redis_val(val) }))
167
+ end
168
+
169
+ def unshift(*x)
170
+ lpush(*(x.map {|val| self.class.to_redis_val(val) }))
171
+ end
172
+
173
+ def first
174
+ if type == 'list'
175
+ self[0]
176
+ elsif(k = keys.first)
177
+ [k, self[k]]
178
+ end
179
+ end
180
+
181
+ def last
182
+ if type == 'list'
183
+ self[-1]
184
+ elsif(k = keys.last)
185
+ [k, self[k]]
186
+ end
187
+ end
188
+
189
+ def length
190
+ llen
191
+ end
192
+
193
+ def []=(key, val)
194
+ if type == 'list'
195
+ lset(key.to_i, self.class.to_redis_val(val))
196
+ else
197
+ hset(key.to_s, self.class.to_redis_val(val))
198
+ end
199
+ end
200
+
201
+ def [](key)
202
+ val = if(type == 'list')
203
+ lindex(key.to_i)
204
+ else
205
+ hget(key.to_s)
206
+ end
207
+ self.class.from_redis_val(val) if val
208
+ end
209
+
210
+ def shift
211
+ val = lpop
212
+ self.class.from_redis_val(val) if val
213
+ end
214
+
215
+ def pop
216
+ val = rpop
217
+ self.class.from_redis_val(val) if val
218
+ end
219
+
220
+ def to_s
221
+ "#<#{blombo_key}>"
141
222
  end
142
223
 
143
224
  end
@@ -11,7 +11,7 @@ class TestBlombo < Test::Unit::TestCase
11
11
  @blombo[:flibble] = "test123"
12
12
  @blombo['derp'] = 'test321'
13
13
  @blombo.deep[:firstname] = 'Herp'
14
- @blombo.deep[:lastname] = 'Derpington'
14
+ @blombo.key('deep')[:lastname] = 'Derpington'
15
15
  end
16
16
 
17
17
  def test_defined
@@ -86,11 +86,21 @@ class TestBlombo < Test::Unit::TestCase
86
86
  assert_equal nil, @blombo.marshaltest[:nil]
87
87
  end
88
88
 
89
- def test_redis_ops
90
- @blombo.listy.rpush('job1')
91
- @blombo.listy.rpush('job2')
92
- assert_equal 2, $redis.llen('blombo:test:listy')
93
- assert_equal ['job1', 'job2'], @blombo.listy.lrange(0, -1)
89
+ def test_redis_list_ops
90
+ @blombo.listy.push('job2')
91
+ @blombo.listy << 'job3'
92
+ @blombo.listy.unshift('job1')
93
+ assert_equal 3, $redis.llen('blombo:test:listy')
94
+ assert_equal ['job1', 'job2', 'job3'], @blombo.listy.to_a
95
+ assert_equal 'job1', @blombo.listy.first
96
+ assert_equal 'job3', @blombo.listy.last
97
+ assert_equal 'job1', @blombo.listy.shift
98
+ assert_equal 'job3', @blombo.listy.pop
99
+ assert_equal 1, @blombo.listy.length
100
+
101
+ x = {['a', 'complex'] => 'type'}
102
+ @blombo.listy[0] = x
103
+ assert_equal x, @blombo.listy[0]
94
104
  end
95
105
 
96
106
  def test_redis_list_type
@@ -106,4 +116,21 @@ class TestBlombo < Test::Unit::TestCase
106
116
  assert_equal Blombo.asdf, Blombo.asdf
107
117
  end
108
118
 
119
+ def test_to_s
120
+ assert_equal '#<blombo:test>', @blombo.to_s
121
+ end
122
+
123
+ def test_hash_setter
124
+
125
+ @blombo.myhash = {:a => 1}
126
+ assert_equal 1, @blombo.myhash[:a]
127
+ end
128
+
129
+ def test_array_setter
130
+ @blombo.myarray << 'test'
131
+ @blombo.myarray = [:one, :two]
132
+ assert_equal :one, @blombo.myarray.first
133
+ assert_equal :two, @blombo.myarray.last
134
+ end
135
+
109
136
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blombo
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-19 00:00:00.000000000 Z
12
+ date: 2012-04-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &78225710 !ruby/object:Gem::Requirement
16
+ requirement: &79947130 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *78225710
24
+ version_requirements: *79947130
25
25
  description: ! 'Blombo: Treat redis-server like a deep ruby hash'
26
26
  email: andrew@modulus.org
27
27
  executables: []