Class: Tempora::Quarter

Inherits:
TimePeriod show all
Extended by:
Initialization
Includes:
HasMonths, HasWeeks
Defined in:
lib/quarter.rb

Instance Attribute Summary collapse

Attributes inherited from TimePeriod

#end_date, #start_date

Instance Method Summary collapse

Methods included from Initialization

from, now

Methods included from HasWeeks

#each_week, #weeks

Methods included from HasMonths

#each_month, #months

Methods inherited from TimePeriod

#<=>, #contains?, #duration, #hash, #intersection, #overlaps?, #range, #shift

Methods included from HasTime

#end_time, #start_time

Methods included from HasDays

#days, #each_day

Constructor Details

#initialize(year, quarter) ⇒ Quarter

Returns a new instance of Quarter.



14
15
16
17
18
19
20
21
# File 'lib/quarter.rb', line 14

def initialize(year, quarter)
  @year   = Integer(year)
  @number = Integer(quarter).clamp(1,4)

  start_month = (quarter - 1) * 3 + 1
  @start_date = Date.new(year, start_month, 1)
  @end_date   = @start_date.next_month(3).prev_day
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



12
13
14
# File 'lib/quarter.rb', line 12

def number
  @number
end

#yearObject (readonly)

Returns the value of attribute year.



12
13
14
# File 'lib/quarter.rb', line 12

def year
  @year
end

Instance Method Details

#id(seperator = "-") ⇒ Object



23
24
25
# File 'lib/quarter.rb', line 23

def id(seperator="-")
  "#{year}#{seperator}Q#{number}"
end

#nextObject Also known as: succ



31
32
33
# File 'lib/quarter.rb', line 31

def next
  self.class.from(start_date >> 3)
end

#prevObject Also known as: pred



35
36
37
# File 'lib/quarter.rb', line 35

def prev
  self.class.from(start_date << 3)
end

#to_sObject



27
28
29
# File 'lib/quarter.rb', line 27

def to_s
  "#{I18n.t('timeperiod.quarter')} #{number}, #{year}"
end