Class: Chicago::Data::Month
- Inherits:
-
Object
- Object
- Chicago::Data::Month
- 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
-
#name ⇒ Object
readonly
Returns the full English name of this month.
-
#short_name ⇒ Object
readonly
Returns the first three letters of the English name of this month.
Class Method Summary collapse
- .find_month_by_name(identifier) ⇒ Object
- .is_month_number?(identifier) ⇒ Boolean
-
.parse(identifier) ⇒ Object
Returns a month given a name of the month or an integer between 1 and 12.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Months are ordered.
-
#in(year) ⇒ Object
Returns the first day of this month in the given year as a Date.
-
#initialize(name, number) ⇒ Month
constructor
A new instance of Month.
-
#to_i ⇒ Object
Returns the number of this month from 1 to 12.
-
#to_s ⇒ Object
Returns the full name of this month.
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
#name ⇒ Object (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_name ⇒ Object (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
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_i ⇒ Object
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_s ⇒ Object
Returns the full name of this month.
47 48 49 |
# File 'lib/chicago/data/month.rb', line 47 def to_s name end |