Class: Redistat::Key

Inherits:
Object
  • Object
show all
Includes:
Database, Options
Defined in:
lib/redistat/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options

included, #options, #parse_options, #raw_options

Methods included from Database

#db, included

Constructor Details

#initialize(scope, label_name = nil, time_stamp = nil, opts = {}) ⇒ Key

Returns a new instance of Key.



10
11
12
13
14
15
# File 'lib/redistat/key.rb', line 10

def initialize(scope, label_name = nil, time_stamp = nil, opts = {})
  parse_options(opts)
  self.scope = scope
  self.label = label_name if !label_name.nil?
  self.date = time_stamp ||= Time.now
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



27
28
29
# File 'lib/redistat/key.rb', line 27

def date
  @date
end

#labelObject

Returns the value of attribute label.



45
46
47
# File 'lib/redistat/key.rb', line 45

def label
  @label
end

#scopeObject

Returns the value of attribute scope.



40
41
42
# File 'lib/redistat/key.rb', line 40

def scope
  @scope
end

Instance Method Details

#childrenObject



55
56
57
58
59
60
61
# File 'lib/redistat/key.rb', line 55

def children
  members = db.smembers("#{scope}#{LABEL_INDEX}#{@label}") || [] # older versions of Redis returns nil
  members.map { |member|
    child_label = [@label, member].reject { |i| i.nil? }
    self.class.new(self.scope, child_label.join(Redistat.group_separator), self.date, @options)
  }
end

#default_optionsObject



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

def default_options
  { :depth => :hour }
end

#depthObject



29
30
31
# File 'lib/redistat/key.rb', line 29

def depth
  options[:depth]
end

#groupsObject



70
71
72
73
74
# File 'lib/redistat/key.rb', line 70

def groups
  @groups ||= @label.groups.map do |label|
    self.class.new(@scope, label, self.date, @options)
  end
end

#label_hashObject



47
48
49
# File 'lib/redistat/key.rb', line 47

def label_hash
  @label.hash
end

#parentObject



51
52
53
# File 'lib/redistat/key.rb', line 51

def parent
  @parent ||= self.class.new(self.scope, @label.parent, self.date, @options) unless @label.parent.nil?
end

#prefixObject



17
18
19
20
21
22
# File 'lib/redistat/key.rb', line 17

def prefix
  key = "#{@scope}"
  key << "/#{label.name}" if !label.nil?
  key << ":"
  key
end

#to_s(depth = nil) ⇒ Object



76
77
78
79
80
81
# File 'lib/redistat/key.rb', line 76

def to_s(depth = nil)
  depth ||= @options[:depth]
  key = self.prefix
  key << @date.to_s(depth)
  key
end

#update_indexObject



63
64
65
66
67
68
# File 'lib/redistat/key.rb', line 63

def update_index
  @label.groups.each do |label|
    parent = (label.parent || "")
    db.sadd("#{scope}#{LABEL_INDEX}#{parent}", label.me)
  end
end