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 126 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 118 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 139 def <=>(other) first_date <=> Month.from(other).first_date end
beginning_of_month()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 85 def beginning_of_month first_date.in_time_zone(timezone) end
cover?(t)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 134 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 93 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 89 def end_of_month last_date.in_time_zone(timezone).end_of_day end
eql?(other)
click to toggle source
# File lib/coaster/core_ext/month.rb, line 152 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 148 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 109 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 105 def previous self.class.from(first_date - 1) end
succ()
click to toggle source
Range implement
# File lib/coaster/core_ext/month.rb, line 144 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 101 def to_date_range first_date..last_date end
to_s()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 113 def to_s first_date.strftime('%Y-%m') end
Also aliased as: inspect
to_time_range()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 97 def to_time_range beginning_of_month...(later.beginning_of_month) end
year()
click to toggle source
# File lib/coaster/core_ext/month.rb, line 57 def year Integer(@_year) end