class Month
Attributes
Public Class Methods
Source
# File lib/coaster/core_ext/month.rb, line 8 def from(object, timezone: nil) case object when Month object.timezone = timezone object when String then Month.parse(object, timezone: timezone) when Array then Month.new(object[0], object[1], timezone: timezone) else new(object.year, object.month, timezone: timezone) end end
Source
# File lib/coaster/core_ext/month.rb, line 46 def initialize(year, month, timezone: nil) @_year = year @_month = month self.timezone = timezone end
Source
# File lib/coaster/core_ext/month.rb, line 21 def parse(str, timezone: nil) date = Date.parse(str) from(date, timezone: timezone) rescue ArgumentError => e if str.instance_variable_defined?(:@_gsub_) && str.instance_variable_get(:@_gsub_) raise e, str: str.instance_variable_get(:@_gsub_) elsif e.message != 'invalid date' raise e, str: str end str_gsub = str.gsub(/[^\d]/, '') str_gsub.insert(4, '0') if str_gsub.length == 5 str_gsub += '01' str_gsub.instance_variable_set(:@_gsub_, str_gsub) parse(str_gsub, timezone: timezone) end
Month.parse
(‘201601’) Month.parse
(‘2016-01’)
Public Instance Methods
Source
# File lib/coaster/core_ext/month.rb, line 143 def +(time) case time when ActiveSupport::Duration then Month.from(first_date.in_time_zone(timezone) + time) else Month.from(first_date + time) end end
Source
# File lib/coaster/core_ext/month.rb, line 135 def -(time) case time when ActiveSupport::Duration then Month.from(first_date.in_time_zone(timezone) - time) else Month.from(first_date - time) end end
Source
# File lib/coaster/core_ext/month.rb, line 156 def <=>(other) first_date <=> Month.from(other).first_date end
Source
# File lib/coaster/core_ext/month.rb, line 85 def _timezone(timezone) tz = timezone || self.timezone tz = ActiveSupport::TimeZone[tz] if tz.is_a?(String) tz end
Source
# File lib/coaster/core_ext/month.rb, line 91 def beginning_of_month(timezone = nil) tz = _timezone(timezone) first_date.in_time_zone(tz) end
Source
# File lib/coaster/core_ext/month.rb, line 110 def beginning_of_range(timezone = nil) to_time_range(timezone).begin end
Source
# File lib/coaster/core_ext/month.rb, line 151 def cover?(t) to_time_range.cover?(t) end
Source
# File lib/coaster/core_ext/month.rb, line 101 def date_for_day(number) Date.new(year, month, number) end
Source
# File lib/coaster/core_ext/month.rb, line 73 def each_date(&block) (first_date..last_date).each(&block) end
Source
# File lib/coaster/core_ext/month.rb, line 96 def end_of_month tz = _timezone(timezone) last_date.in_time_zone(tz).end_of_day end
Source
# File lib/coaster/core_ext/month.rb, line 114 def end_of_range(timezone = nil) to_time_range(timezone).end end
Source
# File lib/coaster/core_ext/month.rb, line 169 def eql?(other) other.is_a?(Month) && first_date == other.first_date end
Source
# File lib/coaster/core_ext/month.rb, line 65 def first_date @first_date ||= Date.new(year, month, 1) end
Source
# File lib/coaster/core_ext/month.rb, line 69 def last_date @last_date ||= Date.new(year, month, -1) end
Source
# File lib/coaster/core_ext/month.rb, line 126 def later self.class.from(last_date + 1) end
Source
# File lib/coaster/core_ext/month.rb, line 122 def previous self.class.from(first_date - 1) end
Source
# File lib/coaster/core_ext/month.rb, line 52 def timezone=(tz) tz = ActiveSupport::TimeZone[tz] if tz.is_a?(String) @timezone = tz || Time.zone end
Source
# File lib/coaster/core_ext/month.rb, line 118 def to_date_range first_date..last_date end
Source
# File lib/coaster/core_ext/month.rb, line 130 def to_s first_date.strftime('%Y-%m') end
Also aliased as: inspect
Source
# File lib/coaster/core_ext/month.rb, line 105 def to_time_range(timezone = nil) tz = _timezone(timezone) beginning_of_month(tz)...(later.beginning_of_month(tz)) end