Class: Widescreen::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/widescreen/stat.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric, time, value = 1) ⇒ Stat

Returns a new instance of Stat.



5
6
7
8
9
# File 'lib/widescreen/stat.rb', line 5

def initialize(metric, time, value = 1)
  @metric = Widescreen::Metric.find_or_create(metric)
  @time   = time
  @value  = value
end

Instance Attribute Details

#metricObject

Returns the value of attribute metric.



3
4
5
# File 'lib/widescreen/stat.rb', line 3

def metric
  @metric
end

#timeObject

Returns the value of attribute time.



3
4
5
# File 'lib/widescreen/stat.rb', line 3

def time
  @time
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/widescreen/stat.rb', line 3

def value
  @value
end

Class Method Details

.add(metric, value = 1) ⇒ Object



15
16
17
# File 'lib/widescreen/stat.rb', line 15

def self.add(metric, value = 1)
  new(metric, Time.now, value).save
end

.find(key) ⇒ Object



19
20
21
22
23
24
# File 'lib/widescreen/stat.rb', line 19

def self.find(key)
  value = Widescreen.redis.get(key)
  return if value.nil?
  metric_name, time = key.split(Widescreen::SEPARATOR, 2)
  new(metric_name, Time.parse(time), value)
end

Instance Method Details

#saveObject



11
12
13
# File 'lib/widescreen/stat.rb', line 11

def save
  Widescreen.redis.incrby(key, value)
end