Class: RedisScanner::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_scanner/pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Pattern

Returns a new instance of Pattern.



37
38
39
40
41
# File 'lib/redis_scanner/pattern.rb', line 37

def initialize(name)
  @name = name
  @total = 0
  @items = Hash.new {|hash, key| hash[key] = PatternItem.new(key) }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/redis_scanner/pattern.rb', line 34

def name
  @name
end

#totalObject

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/redis_scanner/pattern.rb', line 50

def <=>(other)
  if @total == other.total
    @name <=> other.name
  else
    other.total <=> @total
  end
end

#increment(type = nil, size = nil) ⇒ Object



43
44
45
46
47
48
# File 'lib/redis_scanner/pattern.rb', line 43

def increment(type = nil, size = nil)
  @total += 1
  if type && size
    @items[type].increment(size)
  end
end

#sorted_itemsObject



73
74
75
# File 'lib/redis_scanner/pattern.rb', line 73

def sorted_items
  @items.values.sort
end

#to_aObject



58
59
60
# File 'lib/redis_scanner/pattern.rb', line 58

def to_a
  [name, total]
end

#to_sObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/redis_scanner/pattern.rb', line 62

def to_s
  if @items.size > 0
    ret = sorted_items.map do |item|
      "#{@name} #{item}"
    end.join("\n")
  else
    ret = "#{@name} #{@total}"
  end
  ret
end