Class: Quarter

Inherits:
Object
  • Object
show all
Includes:
DateRange
Defined in:
lib/funtimes/quarter.rb

Constant Summary collapse

@@months =
[1, 4, 7, 10]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DateRange

#&, #days, #encompasses, #intersects, #months, #number_of_days_in, #quarters, #same_range_as, #weeks, #|

Constructor Details

#initialize(q, y) ⇒ Quarter

Returns a new instance of Quarter.



6
7
8
9
10
11
# File 'lib/funtimes/quarter.rb', line 6

def initialize(q,y) 
  @number, @year = q, y
  @start_date = DateTime.new(y, @@months[q-1], 1)
  @end_date = DateTime.new(y, @@months[q], 1) - 1 unless q == 4
  @end_date = DateTime.new(y+1, 1, 1) - 1 if q == 4
end

Instance Attribute Details

#end_dateObject

Returns the value of attribute end_date.



3
4
5
# File 'lib/funtimes/quarter.rb', line 3

def end_date
  @end_date
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/funtimes/quarter.rb', line 3

def number
  @number
end

#start_dateObject

Returns the value of attribute start_date.



3
4
5
# File 'lib/funtimes/quarter.rb', line 3

def start_date
  @start_date
end

#yearObject

Returns the value of attribute year.



3
4
5
# File 'lib/funtimes/quarter.rb', line 3

def year
  @year
end

Class Method Details

.from(date) ⇒ Object



13
14
15
16
# File 'lib/funtimes/quarter.rb', line 13

def self.from(date)
  number = @@months.index(@@months.reverse.detect{|i| i <= date.month}) + 1
  new(number, date.year)
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=> (other)
  @start_date <=> other.start_date 
end

#succObject



18
19
20
21
# File 'lib/funtimes/quarter.rb', line 18

def succ
  return Quarter.new(@number+1, @year) unless @number == 4
  Quarter.new(1, @year+1)
end

#to_sObject



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

def to_s
  "Q#{@number}, #{@year}"
end