Class: Von::Period

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

Constant Summary collapse

PERIOD_MAPPING =
{
  :minutely => :minute,
  :hourly   => :hour,
  :daily    => :day,
  :weekly   => :week,
  :monthly  => :month,
  :yearly   => :year
}
AVAILABLE_PERIODS =
PERIOD_MAPPING.keys
AVAILABLE_TIME_UNITS =
PERIOD_MAPPING.values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period, length = nil) ⇒ Period

Initialize a Period object

period - the time period one of AVAILABLE_PERIODS length - length of period



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/von/period.rb', line 22

def initialize(period, length = nil)
  name = period.to_sym
  if AVAILABLE_PERIODS.include?(name)
    @name = name
  elsif AVAILABLE_TIME_UNITS.include?(name)
    @name = PERIOD_MAPPING.invert[name]
  else
    raise ArgumentError, "`#{period}' is not a valid period"
  end
  @length = length
  @format = Von.config.send(:"#{@name}_format")
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



16
17
18
# File 'lib/von/period.rb', line 16

def format
  @format
end

#lengthObject (readonly)

Returns the value of attribute length.



15
16
17
# File 'lib/von/period.rb', line 15

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/von/period.rb', line 14

def name
  @name
end

Class Method Details

.exists?(period) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/von/period.rb', line 71

def self.exists?(period)
  AVAILABLE_PERIODS.include?(period)
end

.time_unit_exists?(time_unit) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/von/period.rb', line 75

def self.time_unit_exists?(time_unit)
  AVAILABLE_TIME_UNITS.include?(time_unit)
end

.unit_to_period(time_unit) ⇒ Object



67
68
69
# File 'lib/von/period.rb', line 67

def self.unit_to_period(time_unit)
  PERIOD_MAPPING.invert[time_unit]
end

Instance Method Details

#beginning(time) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/von/period.rb', line 51

def beginning(time)
  if minutes?
    time.change(:seconds => 0)
  else
    time.send(:"beginning_of_#{time_unit}")
  end
end

#hours?Boolean

Returns True or False if the period is hourly

Returns:

  • (Boolean)


42
43
44
# File 'lib/von/period.rb', line 42

def hours?
  @name == :hourly
end

#minutes?Boolean

Returns True or False if the period is minutely

Returns:

  • (Boolean)


47
48
49
# File 'lib/von/period.rb', line 47

def minutes?
  @name == :minutely
end

#prev(unit = 1) ⇒ Object



59
60
61
# File 'lib/von/period.rb', line 59

def prev(unit = 1)
  beginning(unit.send(time_unit.to_sym).ago).strftime(@format)
end

#time_unitObject

Returns a Symbol representing the time unit for the current period.



37
38
39
# File 'lib/von/period.rb', line 37

def time_unit
  @time_unit ||= PERIOD_MAPPING[@name]
end

#timestampObject



63
64
65
# File 'lib/von/period.rb', line 63

def timestamp
  beginning(Time.now).strftime(format)
end