Module: Seabright::Matchers::ClassMethods

Defined in:
lib/redis_object/matchers.rb

Constant Summary collapse

NilPattern =
'nilpattern:'
GetKeyList =
"local itms
if KEYS[2] then
  itms = {}
  for n=2,#KEYS do
    local kys = redis.call('SMEMBERS',KEYS[n])
    for k=1,#kys do
      local rk
      if kys[k]:match('.*_h') then
        rk = kys[k]
      else
        rk = kys[k]..'_h'
      end
      table.insert(itms, rk)
    end
  end
else
  itms = {}
  local kys = redis.call('SMEMBERS',KEYS[1])
  for k=1,#kys do
    local rk
    if kys[k]:match('.*_h') then
      rk = kys[k]
    else
      rk = kys[k]..'_h'
    end
    table.insert(itms, rk)
  end
end".gsub(/\t/,'').freeze

Instance Method Summary collapse

Instance Method Details

#convert_regex_to_lua(reg) ⇒ Object



450
451
452
# File 'lib/redis_object/matchers.rb', line 450

def convert_regex_to_lua(reg)
  "#{reg.casefold? ? "i" : ""}pattern:#{reg.source.gsub("\\","")}"
end

#extract_usable_indices(pkt) ⇒ Object



373
374
375
376
377
378
379
380
# File 'lib/redis_object/matchers.rb', line 373

def extract_usable_indices(pkt)
  pkt.inject([]) do |acc,(k,v)|
    if self.has_index?(k)
      acc << index_key(k,v)
    end
    acc
  end
end

#find(ident) ⇒ Object



472
473
474
# File 'lib/redis_object/matchers.rb', line 472

def find(ident)
  grab(ident)
end

#find_first(ident) ⇒ Object



476
477
478
# File 'lib/redis_object/matchers.rb', line 476

def find_first(ident)
  match_first(ident)
end

#grab(ident) ⇒ Object



454
455
456
457
458
459
460
461
462
# File 'lib/redis_object/matchers.rb', line 454

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

#inject_key(key, list) ⇒ Object



437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/redis_object/matchers.rb', line 437

def inject_key(key,list)
  out = []
  list.each do |i|
    if i == list.first
      out << i
    else
      out << key
      out << i
    end
  end
  out
end

#match(pkt, use_or = false) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/redis_object/matchers.rb', line 192

def match(pkt, use_or=false)
  if use_or
    mtchr = :OrMatcher
  else
    mtchr = pkt.keys.count > 1 ? :MultiMatcher : :Matcher
  end
  if (ids = pkt[id_sym] || pkt[id_sym.to_s]) and !ids.is_a?(Regexp)
    return match_by_id(ids, pkt, use_or)
  end
  indcs = [plname] + extract_usable_indices(pkt)
  pkt = pkt.flatten.reduce([]) do |i,v|
    x = case v
    when Regexp
      convert_regex_to_lua(v)
    when Array
      raise ArgumentError.new("An array can only be used with the find_or method") unless use_or
      inject_key(i.last, v)
    when NilClass
      NilPattern
    else
      v.to_s
    end
    i << x
    i
  end
  kys = run_script(mtchr, indcs, pkt.flatten)
  ListEnumerator.new(kys) do |y|
    kys.each do |k|
      y << find(k)
    end
  end
end

#match_by_id(ids, pkt, use_or = false) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/redis_object/matchers.rb', line 382

def match_by_id(ids, pkt, use_or = false)
  case ids
  when Array
    raise ArgumentError.new("An array can only be used with the or_find_first method") unless use_or
    ids.map do |i|
      match_by_id(i, pkt, use_or)
    end.flatten
  when String, Symbol
    if obj = find(ids)
      pkt.each do |k,v|
        case v
        when Regexp
          return nil unless v.match(obj.get(k))
        when Array
          raise ArgumentError.new("An array can only be used with the or_find_first method") unless use_or
          return nil unless v.any? { |val| obj.get(k) == v }
        when String, Symbol
          return nil unless obj.get(k) == v
        when NilClass
          return nil unless obj.get(k) == nil
        end
      end
    end
    [obj]
  end
end

#match_first(pkt, use_or = false) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/redis_object/matchers.rb', line 409

def match_first(pkt, use_or=false)
  if use_or
    mtchr = :FirstOrMatcher
  else
    mtchr = pkt.keys.count > 1 ? :FirstMultiMatcher : :FirstMatcher
  end
  if (ids = pkt[id_sym] || pkt[id_sym.to_s]) and !ids.is_a?(Regexp)
    return match_by_id(ids, pkt, use_or).first
  end
  indcs = [plname] + extract_usable_indices(pkt)
  pkt = pkt.flatten.reduce([]) do |i,v|
    x = case v
    when Regexp
      convert_regex_to_lua(v)
    when Array
      raise ArgumentError.new("An array can only be used with the or_find_first method") unless use_or
      inject_key(i.last, v)
    when NilClass
      NilPattern
    else
      v.to_s
    end
    i << x
    i
  end
  find_by_key(run_script(mtchr,indcs, pkt.flatten))
end

#or_find(ident) ⇒ Object



480
481
482
# File 'lib/redis_object/matchers.rb', line 480

def or_find(ident)
  or_grab(ident)
end

#or_find_first(ident) ⇒ Object



484
485
486
# File 'lib/redis_object/matchers.rb', line 484

def or_find_first(ident)
  match_first(ident)
end

#or_grab(ident) ⇒ Object



464
465
466
467
468
469
470
# File 'lib/redis_object/matchers.rb', line 464

def or_grab(ident)
  case ident
  when Hash
    return match(ident, true)
  end
  nil
end