Class: Monthra::Month
- Inherits:
-
Object
- Object
- Monthra::Month
- Includes:
- Comparable
- Defined in:
- lib/monthra/month.rb
Class Method Summary collapse
-
.at(generic_month) ⇒ Month
param [Object] generic_month Must respond to month and year.
-
.current ⇒ Month
For today.
- .strpmonth(month_str, format) ⇒ Month
Instance Method Summary collapse
- #+(offset) ⇒ Month
- #-(offset) ⇒ Month
-
#<=>(other_month) ⇒ Fixnum
1 if greater, 0 if equal, -1 if less.
-
#begin_on ⇒ Date
The first day of the month.
-
#end_on ⇒ Date
The last day of the month.
-
#initialize(new_year, new_month) ⇒ Month
constructor
A new instance of Month.
-
#month ⇒ Fixnum
The month.
- #strfmonth(format) ⇒ Object
-
#to_date ⇒ Date
The first day of the month.
-
#to_monthra_month ⇒ Month
To Match the monkey patch in Date and Time.
-
#to_s ⇒ String
Year-month (padded).
-
#to_time ⇒ Time
The beginning of the month.
-
#year ⇒ Fixnum
The year.
Constructor Details
#initialize(new_year, new_month) ⇒ Month
30 31 32 |
# File 'lib/monthra/month.rb', line 30 def initialize(new_year, new_month) setup_year_and_month(new_year, new_month) end |
Class Method Details
.at(generic_month) ⇒ Month
param [Object] generic_month Must respond to month and year
7 8 9 10 11 12 13 |
# File 'lib/monthra/month.rb', line 7 def self.at(generic_month) if generic_month.respond_to?(:year) && generic_month.respond_to?(:month) self.new(generic_month.year, generic_month.month) else raise Exception("The param must have year and month methods") end end |
.current ⇒ Month
16 17 18 |
# File 'lib/monthra/month.rb', line 16 def self.current self.at(Date.today) end |
.strpmonth(month_str, format) ⇒ Month
23 24 25 |
# File 'lib/monthra/month.rb', line 23 def self.strpmonth(month_str, format) self.at(Time.strptime(month_str, format)) end |
Instance Method Details
#+(offset) ⇒ Month
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/monthra/month.rb', line 92 def +(offset) offsets = month_year_offset(offset) new_year = year + offsets[:year] new_month = month + offsets[:month] if new_month > 12 new_month -= 12 new_year += 1 end self.class.new(new_year, new_month) end |
#-(offset) ⇒ Month
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/monthra/month.rb', line 108 def -(offset) offsets = month_year_offset(offset) new_year = year - offsets[:year] new_month = month - offsets[:month] if new_month < 1 new_month += 12 # note there is no month 0 new_year -= 1 end self.class.new(new_year, new_month) end |
#<=>(other_month) ⇒ Fixnum
36 37 38 39 40 41 42 43 44 |
# File 'lib/monthra/month.rb', line 36 def <=>(other_month) if year == other_month.year && month == other_month.month return 0 elsif year > other_month.year || (year == other_month.year && month > other_month.month) return 1 else # the current month is less return -1 end end |
#begin_on ⇒ Date
72 73 74 |
# File 'lib/monthra/month.rb', line 72 def begin_on to_date end |
#end_on ⇒ Date
77 78 79 |
# File 'lib/monthra/month.rb', line 77 def end_on Date.new(year, month, -1) end |
#month ⇒ Fixnum
57 58 59 |
# File 'lib/monthra/month.rb', line 57 def month @month end |
#strfmonth(format) ⇒ Object
81 82 83 |
# File 'lib/monthra/month.rb', line 81 def strfmonth(format) to_time.strftime(format) end |
#to_date ⇒ Date
67 68 69 |
# File 'lib/monthra/month.rb', line 67 def to_date Date.new(year, month, 1) end |
#to_monthra_month ⇒ Month
47 48 49 |
# File 'lib/monthra/month.rb', line 47 def to_monthra_month self end |
#to_s ⇒ String
86 87 88 |
# File 'lib/monthra/month.rb', line 86 def to_s strfmonth("%Y-%m") end |
#to_time ⇒ Time
62 63 64 |
# File 'lib/monthra/month.rb', line 62 def to_time to_date.to_time end |
#year ⇒ Fixnum
52 53 54 |
# File 'lib/monthra/month.rb', line 52 def year @year end |