Class: Von::Period
- Inherits:
-
Object
- Object
- Von::Period
- 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
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .exists?(period) ⇒ Boolean
- .time_unit_exists?(time_unit) ⇒ Boolean
- .unit_to_period(time_unit) ⇒ Object
Instance Method Summary collapse
- #beginning(time) ⇒ Object
-
#hours? ⇒ Boolean
Returns True or False if the period is hourly.
-
#initialize(period, length = nil) ⇒ Period
constructor
Initialize a Period object.
-
#minutes? ⇒ Boolean
Returns True or False if the period is minutely.
- #prev(unit = 1) ⇒ Object
-
#time_unit ⇒ Object
Returns a Symbol representing the time unit for the current period.
- #timestamp ⇒ Object
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
#format ⇒ Object (readonly)
Returns the value of attribute format.
16 17 18 |
# File 'lib/von/period.rb', line 16 def format @format end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
15 16 17 |
# File 'lib/von/period.rb', line 15 def length @length end |
#name ⇒ Object (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
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
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
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
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_unit ⇒ Object
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 |
#timestamp ⇒ Object
63 64 65 |
# File 'lib/von/period.rb', line 63 def beginning(Time.now).strftime(format) end |