Class: Lafcadio::Month

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/lafcadio/dateTime/Month.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(month = nil, year = 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.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lafcadio/dateTime/Month.rb', line 19

def initialize( month = nil, year = 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.



15
16
17
# File 'lib/lafcadio/dateTime/Month.rb', line 15

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



15
16
17
# File 'lib/lafcadio/dateTime/Month.rb', line 15

def year
  @year
end

Class Method Details

.monthNamesObject

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



8
9
10
11
# File 'lib/lafcadio/dateTime/Month.rb', line 8

def Month.monthNames
	[ "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.



57
58
59
60
61
62
63
64
65
66
# File 'lib/lafcadio/dateTime/Month.rb', line 57

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

#-(amountToSubtract) ⇒ Object

Returns a new Month that is amountToSubtract months earlier.



69
70
71
# File 'lib/lafcadio/dateTime/Month.rb', line 69

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

#<=>(anOther) ⇒ Object

Compare this Month to another Month.



32
33
34
35
36
37
38
# File 'lib/lafcadio/dateTime/Month.rb', line 32

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

#endDateObject

Returns the last Date of the month.



89
90
91
# File 'lib/lafcadio/dateTime/Month.rb', line 89

def endDate
	self.next.startDate - 1
end

#eql?(anOther) ⇒ Boolean

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/lafcadio/dateTime/Month.rb', line 52

def eql?(anOther)
	self == anOther
end

#hashObject

Calculate a hash value for this Month.



46
47
48
# File 'lib/lafcadio/dateTime/Month.rb', line 46

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

#nextObject

Returns the next Month.



79
80
81
# File 'lib/lafcadio/dateTime/Month.rb', line 79

def next
	self + 1
end

#prevObject

Returns the previous Month.



74
75
76
# File 'lib/lafcadio/dateTime/Month.rb', line 74

def prev
	self - 1
end

#startDateObject

Returns the first Date of the month.



84
85
86
# File 'lib/lafcadio/dateTime/Month.rb', line 84

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

#to_sObject

Returns a string of the format “January 2001”.



41
42
43
# File 'lib/lafcadio/dateTime/Month.rb', line 41

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