Class: Blombo

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/blombo.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, blombo_parent = nil) ⇒ Blombo

Returns a new instance of Blombo.



52
53
54
55
# File 'lib/blombo.rb', line 52

def initialize(name, blombo_parent=nil)
  @name = name.to_s
  @blombo_parent = blombo_parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *params, &bl) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/blombo.rb', line 128

def method_missing(meth, *params, &bl)
  if Blombo.redis.respond_to?(meth)
    Blombo.redis.send(meth, blombo_key, *params, &bl)
  elsif params.empty? && meth =~ /^[a-z_][a-z0-9_]*$/i
    key(meth)
  elsif params.length == 1 && meth =~ /^(.*)=$/
    if Hash === params[0]
      key($1).del
      params[0].each {|k,val| key($1)[k] = val }
    elsif Enumerable === params[0]
      key($1).del
      params[0].each {|val| key($1).push(val) }
    else
      raise TypeError.new('Blombo setters must be sent a Hash or Array')
    end
  else
    super(meth, *params, &bl)
  end
end

Class Attribute Details

.redisObject



10
11
12
13
14
# File 'lib/blombo.rb', line 10

def redis
  Thread.current[:blombo_redis] ||= Redis.new(Hash[*([
    :path, :host, :port, :db, :timeout, :password, :logger
  ].map {|f| [f, @redis.client.send(f)] }.flatten)])
end

Instance Attribute Details

#blombo_parentObject (readonly)

Returns the value of attribute blombo_parent.



6
7
8
# File 'lib/blombo.rb', line 6

def blombo_parent
  @blombo_parent
end

Class Method Details

.[](name) ⇒ Object



35
36
37
# File 'lib/blombo.rb', line 35

def [](name)
  new(name)
end

.from_redis_val(str) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/blombo.rb', line 26

def from_redis_val(str)
  if(is_marshalled?(str))
    Marshal.load(str)
  elsif(str =~ /^\d+$/)
    str.to_i
  else
    str
  end
end

.is_marshalled?(str) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/blombo.rb', line 16

def is_marshalled?(str)
  Marshal.dump(nil)[0,2] == str[0,2] # Marshall stores its version info in first 2 bytes
end

.method_missing(meth, *params, &bl) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/blombo.rb', line 38

def method_missing(meth, *params, &bl)
  if params.empty? && meth =~ /^[a-z_][a-z0-9_]*$/i
    new meth
  else
    super
  end
end

.to_redis_val(obj) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/blombo.rb', line 19

def to_redis_val(obj)
  if Integer===obj || String===obj && obj !~ /^\d+$/ && !is_marshalled?(obj)
    obj.to_s
  else
    Marshal.dump(obj)
  end
end

Instance Method Details

#<<(x) ⇒ Object



161
162
163
# File 'lib/blombo.rb', line 161

def <<(x)
  push(x)
end

#<=>(other) ⇒ Object



67
68
69
# File 'lib/blombo.rb', line 67

def <=>(other)
  @name <=> other.blombo_name
end

#[](key) ⇒ Object



201
202
203
204
205
206
207
208
# File 'lib/blombo.rb', line 201

def [](key)
  val = if(type == 'list')
    lindex(key.to_i)
  else
    hget(key.to_s)
  end
  self.class.from_redis_val(val) if val
end

#[]=(key, val) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/blombo.rb', line 193

def []=(key, val)
  if type == 'list'
    lset(key.to_i, self.class.to_redis_val(val))
  else
    hset(key.to_s, self.class.to_redis_val(val))
  end
end

#blombo_childrenObject



152
153
154
# File 'lib/blombo.rb', line 152

def blombo_children
  blombo_children_names.map {|k| Blombo.new(k, self) }
end

#blombo_children_namesObject



156
157
158
# File 'lib/blombo.rb', line 156

def blombo_children_names
  self.class.redis.keys("#{blombo_key}:*").map {|k| k.gsub(/^blombo:/,'') }
end

#blombo_keyObject



71
72
73
# File 'lib/blombo.rb', line 71

def blombo_key
  "blombo:#{@name}"
end

#blombo_nameObject



75
76
77
# File 'lib/blombo.rb', line 75

def blombo_name
  @name
end

#clearObject



120
121
122
# File 'lib/blombo.rb', line 120

def clear
  redis.del(blombo_key)
end

#defined?(key) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/blombo.rb', line 80

def defined?(key)
  redis.exists(key.to_s)
end

#each(*args, &bl) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/blombo.rb', line 96

def each(*args, &bl)
  if(type == 'list')
    lrange(0, -1).map {|va| each(*args, &bl) }
  else
    to_hash.each(*args, &bl)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/blombo.rb', line 88

def empty?
  redis.hlen(blombo_key) == 0
end

#firstObject



173
174
175
176
177
178
179
# File 'lib/blombo.rb', line 173

def first
  if type == 'list'
    self[0]
  elsif(k = keys.first)
    [k, self[k]]
  end
end

#key(*args) ⇒ Object



148
149
150
# File 'lib/blombo.rb', line 148

def key(*args)
  Blombo.new("#{@name}:#{args.join(':')}", self)
end

#keysObject



112
113
114
# File 'lib/blombo.rb', line 112

def keys
  hkeys
end

#lastObject



181
182
183
184
185
186
187
# File 'lib/blombo.rb', line 181

def last
  if type == 'list'
    self[-1]
  elsif(k = keys.last)
    [k, self[k]]
  end
end

#lengthObject



189
190
191
# File 'lib/blombo.rb', line 189

def length
  llen
end

#nil?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/blombo.rb', line 84

def nil?
  empty?
end

#popObject



215
216
217
218
# File 'lib/blombo.rb', line 215

def pop
  val = rpop
  self.class.from_redis_val(val) if val
end

#push(*x) ⇒ Object



165
166
167
# File 'lib/blombo.rb', line 165

def push(*x)
  rpush(*(x.map {|val| self.class.to_redis_val(val) }))
end

#redisObject



48
49
50
# File 'lib/blombo.rb', line 48

def redis
  self.class.redis
end

#shiftObject



210
211
212
213
# File 'lib/blombo.rb', line 210

def shift
  val = lpop
  self.class.from_redis_val(val) if val
end

#to_aObject



104
105
106
107
108
109
110
# File 'lib/blombo.rb', line 104

def to_a
  if(type == 'list')
    lrange(0, -1).map {|val| self.class.from_redis_val(val) }
  else
    to_hash.to_a
  end
end

#to_hashObject



92
93
94
# File 'lib/blombo.rb', line 92

def to_hash
  keys.inject({}) {|h,k| h.merge!(k => self[k]) }
end

#to_sObject



220
221
222
# File 'lib/blombo.rb', line 220

def to_s
  "#<#{blombo_key}>"
end

#typeObject



124
125
126
# File 'lib/blombo.rb', line 124

def type
  @type ||= (t = Blombo.redis.type(blombo_key)) && t != 'none' && t || nil
end

#unshift(*x) ⇒ Object



169
170
171
# File 'lib/blombo.rb', line 169

def unshift(*x)
  lpush(*(x.map {|val| self.class.to_redis_val(val) }))
end

#valuesObject



116
117
118
# File 'lib/blombo.rb', line 116

def values
  hvals.map {|v| self.class.from_redis_val(v) }
end

#with_timeout(secs) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/blombo.rb', line 57

def with_timeout(secs)
  begin
    Timeout.timeout(secs) do
      yield(self)
    end
  rescue Timeout::Error
    false
  end
end