Class: Month

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/month.rb

Overview

Month represents a specific month in time. With the exception of Month.month_names (which returns a zero-based array), every usage of the month value assumes that 1 equals January and 12 equals December.

Constant Summary collapse

Version =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = nil, month = nil) ⇒ Month

A new month can be set to a specific month and year, or you can call Month.new with no arguments to receive the current month.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/month.rb', line 20

def initialize( year = nil, month = nil )
	require 'date'
	if month.nil? || year.nil?
		date = Date.today
		month = date.mon unless month
		year = date.year unless year
	end
	fail "invalid month" if month < 1 || month > 12
	@month = month
	@year = year
end

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



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

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



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

def year
  @year
end

Class Method Details

.month_namesObject

Returns an array of the full names of months (in English). Note that “January” is the 0th element, and “December” is the 11th element.



9
10
11
12
# File 'lib/month.rb', line 9

def Month.month_names
	[ "January", "February", "March", "April", "May", "June", "July",
		"August", "September", "October", "November", "December" ]
end

Instance Method Details

#+(amountToAdd) ⇒ Object

Returns a new Month that is amountToAdd months later.



33
34
35
36
37
38
39
40
41
42
# File 'lib/month.rb', line 33

def +( amountToAdd )
	( fullYears, remainingMonths ) = amountToAdd.divmod( 12 )
	resultYear = @year + fullYears
	resultMonth = @month + remainingMonths
	if resultMonth > 12
		resultMonth -= 12
		resultYear += 1
	end
	Month.new( resultYear, resultMonth )
end

#-(amountToSubtract) ⇒ Object

Returns a new Month that is amountToSubtract months earlier.



45
46
47
# File 'lib/month.rb', line 45

def -(amountToSubtract)
	self + (-amountToSubtract)
end

#<=>(anOther) ⇒ Object

Compare this Month to another Month.



50
51
52
53
54
55
56
# File 'lib/month.rb', line 50

def <=>(anOther)
	if @year == anOther.year
		@month <=> anOther.month
	else
		@year <=> anOther.year
	end
end

#end_dateObject

Returns the last Date of the month.



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

def end_date
	self.next.start_date - 1
end

#eql?(anOther) ⇒ Boolean

Is this Month equal to anOther? anOther must be another Month of the same value.

Returns:

  • (Boolean)


65
66
67
# File 'lib/month.rb', line 65

def eql?(anOther)
	self == anOther
end

#hashObject

Calculate a hash value for this Month.



70
71
72
# File 'lib/month.rb', line 70

def hash
	"#{@year}#{@month}".to_i
end

#nextObject

Returns the next Month.



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

def next
	self + 1
end

#prevObject

Returns the previous Month.



80
81
82
# File 'lib/month.rb', line 80

def prev
	self - 1
end

#start_dateObject

Returns the first Date of the month.



85
86
87
# File 'lib/month.rb', line 85

def start_date
	Date.new( @year, @month, 1 )
end

#to_sObject

Returns a string of the format “January 2001”.



90
91
92
# File 'lib/month.rb', line 90

def to_s
	Month.month_names[@month-1][0..2] + " " + @year.to_s
end