class Monthra::Month
Public Class Methods
param [Object] generic_month Must respond to month and year @return [Month]
# 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
@return [Month] For today
# File lib/monthra/month.rb, line 16 def self.current self.at(Date.today) end
@param [Fixnum] new_year The year of the month @param [Fixnum] new_month The month of the year. If a negative month is passed in, the year
is decremented.
# File lib/monthra/month.rb, line 30 def initialize(new_year, new_month) setup_year_and_month(new_year, new_month) end
@param [String] month_str @param [String] format @return [Month]
# File lib/monthra/month.rb, line 23 def self.strpmonth(month_str, format) self.at(Time.strptime(month_str, format)) end
Public Instance Methods
@param [Integer or Month] offset How much to add to the current month. @return [Month]
# 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
@param [Integer or Month] offset How much to subtract to the current month. @return [Month]
# 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
@param [Month] other_month The month to compare to @return [Fixnum] 1 if greater, 0 if equal, -1 if less
# 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
@return [Date] The first day of the month
# File lib/monthra/month.rb, line 72 def begin_on to_date end
@return [Date] The last day of the month
# File lib/monthra/month.rb, line 77 def end_on Date.new(year, month, -1) end
@return [Fixnum] The month
# File lib/monthra/month.rb, line 57 def month @month end
# File lib/monthra/month.rb, line 81 def strfmonth(format) to_time.strftime(format) end
@return [Date] The first day of the month
# File lib/monthra/month.rb, line 67 def to_date Date.new(year, month, 1) end
@return [String] year-month (padded)
# File lib/monthra/month.rb, line 86 def to_s strfmonth("%Y-%m") end
@return [Time] The beginning of the month
# File lib/monthra/month.rb, line 62 def to_time to_date.to_time end
@return [Fixnum] The year
# File lib/monthra/month.rb, line 52 def year @year end
Private Instance Methods
@param [Integer or Month] offset How much to add to the current month. If a month
object is passed in, both the year and month values are considered. If an integer is passed in, it represents the number of months.
@return [Hash] With :year and :month keys with offsets
# File lib/monthra/month.rb, line 128 def month_year_offset(offset) if offset.is_a?(Integer) year_offset = offset / 12 month_offset = offset % 12 elsif offset.is_a?(Month) year_offset = offset.year month_offset = offset.month else raise ArgumentError, "Unsupported data type, #{month_offset.class.name}" end return { year: year_offset, month: month_offset } end
if month > 12 or < -12 or == 0, raise exception if the month < 0, then subtract that number from 12 and decrement the year otherwise, just set the month and year
@param [Fixnum] new_year @param [Fixnum] new_month
# File lib/monthra/month.rb, line 151 def setup_year_and_month(new_year, new_month) if new_month > 12 || new_month < -12 || new_month == 0 raise Exception "Month out of range. Allowable values: 1..12 and -12..-1." elsif new_month < 0 @year = new_year - 1 @month = 13 + new_month else @year = new_year @month = new_month end end