Class: Widescreen::Metric

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_and_filter) ⇒ Metric

Returns a new instance of Metric.



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

def initialize(name_and_filter)
  @name, @filter = name_and_filter.split(Widescreen::SEPARATOR, 2)
  @filter = "#{@filter}*"
end

Instance Attribute Details

#filterObject

Returns the value of attribute filter.



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

def filter
  @filter
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.allObject



48
49
50
# File 'lib/widescreen/metric.rb', line 48

def self.all
  Widescreen.redis.smembers(:metrics).map {|m| find(m)}
end

.create(name) ⇒ Object



52
53
54
55
56
# File 'lib/widescreen/metric.rb', line 52

def self.create(name)
  metric = new(name)
  metric.save
  metric
end

.find(name) ⇒ Object



32
33
34
# File 'lib/widescreen/metric.rb', line 32

def self.find(name)
  new(name)
end

.find_or_create(metric) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/widescreen/metric.rb', line 40

def self.find_or_create(metric)
  if Widescreen.redis.sismember(:metrics, metric)
    find(metric)
  else
    create(metric)
  end
end

Instance Method Details

#new_record?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/widescreen/metric.rb', line 10

def new_record?
  !Widescreen.redis.sismember(:metrics, name)
end

#push(value) ⇒ Object



24
25
26
# File 'lib/widescreen/metric.rb', line 24

def push(value)
  Widescreen::Stat.add(self, value)
end

#saveObject



14
15
16
17
18
# File 'lib/widescreen/metric.rb', line 14

def save
  return false unless valid?
  Widescreen.redis.sadd(:metrics, name) if new_record?
  true
end

#statsObject



28
29
30
# File 'lib/widescreen/metric.rb', line 28

def stats
  Widescreen.redis.keys([name, filter].join(Widescreen::SEPARATOR)).map { |s| Widescreen::Stat.find(s) }
end

#to_sObject



36
37
38
# File 'lib/widescreen/metric.rb', line 36

def to_s
  name
end

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/widescreen/metric.rb', line 20

def valid?
  !name.nil? && !name.empty?
end