Class: Von::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/von/counter.rb

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ Counter

Returns a new instance of Counter.



4
5
6
# File 'lib/von/counter.rb', line 4

def initialize(field)
  @field = field.to_sym
end

Instance Method Details

#best(unit) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/von/counter.rb', line 38

def best(unit)
  periods = Von.config.bests[@field]

  if !Period.time_unit_exists?(unit)
    raise ArgumentError, "`#{unit}' is an unknown time unit"
  else
    Counters::Best.new(@field, periods).count(unit)
  end
rescue Redis::BaseError => e
  raise e if Von.config.raise_connection_errors
end

#per(unit) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/von/counter.rb', line 26

def per(unit)
  periods = Von.config.periods[@field]

  if !Period.time_unit_exists?(unit)
    raise ArgumentError, "`#{unit}' is an unknown time unit"
  else
    Counters::Period.new(@field, periods).count(unit)
  end
rescue Redis::BaseError => e
  raise e if Von.config.raise_connection_errors
end

#this(unit) ⇒ Object Also known as: current



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/von/counter.rb', line 50

def this(unit)
  periods = Von.config.currents[@field]

  if !Period.time_unit_exists?(unit)
    raise ArgumentError, "`#{unit}' is an unknown time unit"
  else
    Counters::Current.new(@field, periods).count(unit)
  end
rescue Redis::BaseError => e
  raise e if Von.config.raise_connection_errors
end

#to_iObject



14
15
16
17
18
# File 'lib/von/counter.rb', line 14

def to_i
  Counters::Total.new(@field).count
rescue Redis::BaseError => e
  raise e if Von.config.raise_connection_errors
end

#to_sObject



8
9
10
11
12
# File 'lib/von/counter.rb', line 8

def to_s
  Counters::Total.new(@field).count.to_s
rescue Redis::BaseError => e
  raise e if Von.config.raise_connection_errors
end

#todayObject



64
65
66
67
68
# File 'lib/von/counter.rb', line 64

def today
  periods = Von.config.currents[@field]

  Counters::Current.new(@field, periods).count(:day)
end

#totalObject



20
21
22
23
24
# File 'lib/von/counter.rb', line 20

def total
  Counters::Total.new(@field).count
rescue Redis::BaseError => e
  raise e if Von.config.raise_connection_errors
end