class Month
Attributes
_month[R]
_year[R]
timezone[R]
Public Class Methods
current()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 37 def current from(Date.current) end
from(object, timezone: nil)
click to toggle 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
new(year, month, timezone: nil)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 46 def initialize(year, month, timezone: nil) @_year = year @_month = month self.timezone = timezone end
now()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 41 def now from(Time.zone.now) end
parse(str, timezone: nil)
click to toggle source
Month.parse
(‘201601’) Month.parse
(‘2016-01’)
# 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
Public Instance Methods
+(time)
click to toggle 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
-(time)
click to toggle 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
<=>(other)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 156 def <=>(other) first_date <=> Month.from(other).first_date end
_timezone(timezone)
click to toggle 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
beginning_of_month(timezone = nil)
click to toggle 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
beginning_of_range(timezone = nil)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 110 def beginning_of_range(timezone = nil) to_time_range(timezone).begin end
cover?(t)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 151 def cover?(t) to_time_range.cover?(t) end
date_for_day(number)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 101 def date_for_day(number) Date.new(year, month, number) end
each_date(&block)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 73 def each_date(&block) (first_date..last_date).each(&block) end
end_of_month()
click to toggle 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
end_of_range(timezone = nil)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 114 def end_of_range(timezone = nil) to_time_range(timezone).end end
eql?(other)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 169 def eql?(other) other.is_a?(Month) && first_date == other.first_date end
first_date()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 65 def first_date @first_date ||= Date.new(year, month, 1) end
first_day()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 77 def first_day first_date.day end
hash()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 165 def hash first_date.hash end
last_date()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 69 def last_date @last_date ||= Date.new(year, month, -1) end
last_day()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 81 def last_day last_date.day end
later()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 126 def later self.class.from(last_date + 1) end
month()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 61 def month Integer(@_month) end
previous()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 122 def previous self.class.from(first_date - 1) end
succ()
click to toggle source
Range implement
# File lib/coaster/core_ext/month.rb, line 161 def succ later end
timezone=(tz)
click to toggle 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
to_date_range()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 118 def to_date_range first_date..last_date end
to_s()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 130 def to_s first_date.strftime('%Y-%m') end
Also aliased as: inspect
to_time_range(timezone = nil)
click to toggle 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
year()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 57 def year Integer(@_year) end