Module: Seabright::ObjectBase::ClassMethods
- Defined in:
- lib/redis_object/base.rb
Instance Method Summary collapse
- #all ⇒ Object
- #cname ⇒ Object
- #convert_regex_to_lua(reg) ⇒ Object
- #create(ident = {}) ⇒ Object
- #dbnum ⇒ Object
- #deep_const_get(const, base = nil) ⇒ Object
- #describe ⇒ Object
- #each ⇒ Object
- #exists?(k) ⇒ Boolean
- #find(ident) ⇒ Object
- #find_by_key(k) ⇒ Object
- #first ⇒ Object
- #generate_id ⇒ Object
- #grab(ident) ⇒ Object
- #id_sym(cls = cname) ⇒ Object
- #match(pkt) ⇒ Object
- #new_id(complexity = 8) ⇒ Object
- #plname ⇒ Object
- #recollect! ⇒ Object
- #reserve(k) ⇒ Object
- #save_all ⇒ Object
-
#use_dbnum(db = 0) ⇒ Object
def dump out = [] each do |obj| out << obj.dump end out.join(“n”) end.
Instance Method Details
#all ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/redis_object/base.rb', line 246 def all Enumerator.new do |y| store.smembers(plname).each do |member| if a = find_by_key(hkey(member)) y << a else puts "[#{name}] Object listed but not found: #{member}" if DEBUG store.srem(plname,member) end end end end |
#cname ⇒ Object
238 239 240 |
# File 'lib/redis_object/base.rb', line 238 def cname self.name end |
#convert_regex_to_lua(reg) ⇒ Object
351 352 353 |
# File 'lib/redis_object/base.rb', line 351 def convert_regex_to_lua(reg) "pattern:#{reg.source.gsub("\\","")}" end |
#create(ident = {}) ⇒ Object
373 374 375 376 377 |
# File 'lib/redis_object/base.rb', line 373 def create(ident={}) obj = new(ident) obj.save obj end |
#dbnum ⇒ Object
391 392 393 |
# File 'lib/redis_object/base.rb', line 391 def dbnum @dbnum ||= 0 end |
#deep_const_get(const, base = nil) ⇒ Object
402 403 404 405 406 407 408 409 410 |
# File 'lib/redis_object/base.rb', line 402 def deep_const_get(const,base=nil) if Symbol === const const = const.to_s else const = const.to_str.dup end base ||= const.sub!(/^::/, '') ? Object : self const.split(/::/).inject(base) { |mod, name| mod.const_get(name) } end |
#describe ⇒ Object
423 424 425 426 427 428 429 |
# File 'lib/redis_object/base.rb', line 423 def describe all_keys.inject({}) do |acc,(k,v)| acc[k.to_sym] ||= [:string, 0] acc[k.to_sym][1] += 1 acc end end |
#each ⇒ Object
273 274 275 276 277 |
# File 'lib/redis_object/base.rb', line 273 def each all.each do |o| yield o end end |
#exists?(k) ⇒ Boolean
369 370 371 |
# File 'lib/redis_object/base.rb', line 369 def exists?(k) store.exists(self.hkey(k)) || store.exists(self.reserve_key(k)) end |
#find(ident) ⇒ Object
365 366 367 |
# File 'lib/redis_object/base.rb', line 365 def find(ident) grab(ident) end |
#find_by_key(k) ⇒ Object
395 396 397 398 399 400 |
# File 'lib/redis_object/base.rb', line 395 def find_by_key(k) if store.exists(k) && (cls = store.hget(k,:class)) return deep_const_get(cls.to_sym,Object).new(store.hget(k,id_sym(cls))) end nil end |
#first ⇒ Object
265 266 267 268 269 270 271 |
# File 'lib/redis_object/base.rb', line 265 def first if m = store.smembers(plname) self.grab(m.first) else nil end end |
#generate_id ⇒ Object
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/redis_object/base.rb', line 219 def generate_id v = new_id while exists?(v) do puts "[RedisObject] Collision at id: #{v}" if Debug.verbose? v = new_id end puts "[RedisObject] Reserving key: #{v}" if Debug.verbose? reserve(v) v end |
#grab(ident) ⇒ Object
355 356 357 358 359 360 361 362 363 |
# File 'lib/redis_object/base.rb', line 355 def grab(ident) case ident when String, Symbol return store.exists(self.hkey(ident.to_s)) ? self.new(ident.to_s) : nil when Hash return match(ident) end nil end |
#id_sym(cls = cname) ⇒ Object
419 420 421 |
# File 'lib/redis_object/base.rb', line 419 def id_sym(cls=cname) "#{cls.split('::').last.downcase}_id".to_sym end |
#match(pkt) ⇒ Object
343 344 345 346 347 348 349 |
# File 'lib/redis_object/base.rb', line 343 def match(pkt) Enumerator.new do |y| run_script(pkt.keys.count > 1 ? :MultiMatcher : :Matcher,[plname],pkt.flatten.map{|i| i.is_a?(Regexp) ? convert_regex_to_lua(i) : i.to_s }).each do |k| y << find(k) end end end |
#new_id(complexity = 8) ⇒ Object
234 235 236 |
# File 'lib/redis_object/base.rb', line 234 def new_id(complexity = 8) rand(36**complexity).to_s(36) end |
#plname ⇒ Object
242 243 244 |
# File 'lib/redis_object/base.rb', line 242 def plname cname.pluralize end |
#recollect! ⇒ Object
259 260 261 262 263 |
# File 'lib/redis_object/base.rb', line 259 def recollect! store.keys("#{name}:*_h").each do |ky| store.sadd(plname,ky.gsub(/_h$/,'')) end end |
#reserve(k) ⇒ Object
230 231 232 |
# File 'lib/redis_object/base.rb', line 230 def reserve(k) store.set(reserve_key(k),Time.now.to_s) end |
#save_all ⇒ Object
412 413 414 415 416 417 |
# File 'lib/redis_object/base.rb', line 412 def save_all all.each do |obj| obj.save end true end |
#use_dbnum(db = 0) ⇒ Object
def dump out = [] each do |obj| out << obj.dump end out.join(“n”) end
387 388 389 |
# File 'lib/redis_object/base.rb', line 387 def use_dbnum(db=0) @dbnum = db end |