Class: Chicago::Data::Month

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

Overview

A month of the year.

Months cannot be initialized. Instead call the month name:

Chicago::Data::Month.march # => returns a month

or parse a string or a month number:

Chicago::Data::Month.parse("apr")       # => returns April
Chicago::Data::Month.parse("september") # => returns September
Chicago::Data::Month.parse(1)           # => returns January

Constant Summary collapse

ALL =

All twelve months.

[Month.new("January", 1),
Month.new("February", 2),
Month.new("March", 3),
Month.new("April", 4),
Month.new("May", 5),
Month.new("June", 6),
Month.new("July", 7),
Month.new("August", 8),
Month.new("September", 9),
Month.new("October", 10),
Month.new("November", 11),
Month.new("December", 12)].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, number) ⇒ Month

Returns a new instance of Month.



25
26
27
28
29
# File 'lib/chicago/data/month.rb', line 25

def initialize(name, number)
  @name = name
  @short_name = name[0..2]
  @number = number
end

Instance Attribute Details

#nameObject (readonly)

Returns the full English name of this month



19
20
21
# File 'lib/chicago/data/month.rb', line 19

def name
  @name
end

#short_nameObject (readonly)

Returns the first three letters of the English name of this month.



22
23
24
# File 'lib/chicago/data/month.rb', line 22

def short_name
  @short_name
end

Class Method Details

.find_month_by_name(identifier) ⇒ Object



85
86
87
88
89
90
# File 'lib/chicago/data/month.rb', line 85

def find_month_by_name(identifier)
  name = identifier.strip.downcase
  ALL.find do |month|
    month.name.downcase == name || month.short_name.downcase == name
  end
end

.is_month_number?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_month_number?(identifier)
  identifier.kind_of?(Fixnum) && identifier >= 1 && identifier <= 12
end

.parse(identifier) ⇒ Object

Returns a month given a name of the month or an integer between 1 and 12.



72
73
74
75
76
77
78
# File 'lib/chicago/data/month.rb', line 72

def parse(identifier)
  if is_month_number?(identifier)
    ALL[identifier - 1]
  else
    find_month_by_name(identifier)
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Months are ordered.



32
33
34
# File 'lib/chicago/data/month.rb', line 32

def <=>(other)
  to_i <=> other.to_i 
end

#in(year) ⇒ Object

Returns the first day of this month in the given year as a Date.



37
38
39
# File 'lib/chicago/data/month.rb', line 37

def in(year)
  Date.new(year, to_i, 1)
end

#to_iObject

Returns the number of this month from 1 to 12.



42
43
44
# File 'lib/chicago/data/month.rb', line 42

def to_i
  @number
end

#to_sObject

Returns the full name of this month.



47
48
49
# File 'lib/chicago/data/month.rb', line 47

def to_s
  name
end