class DatePeriod::Month

Attributes

month[R]
year[R]

Public Class Methods

current() click to toggle source
# File lib/date_period/month.rb, line 13
def current
  including Date.current
end
dump(month) click to toggle source
# File lib/date_period/month.rb, line 22
def dump(month)
  JSON.dump("year" => month.year, "month" => month.month)
end
including(datish) click to toggle source
# File lib/date_period/month.rb, line 8
def including(datish)
  date = datish.to_date
  new year: date.year, month: date.month
end
load(serialized_month) click to toggle source
# File lib/date_period/month.rb, line 17
def load(serialized_month)
  data = JSON.load serialized_month
  new year: data["year"], month: data["month"]
end
new(year: Date.current.year, month: Date.current.month) click to toggle source
# File lib/date_period/month.rb, line 29
def initialize(year: Date.current.year, month: Date.current.month)
  @year = year
  @month = month
end

Public Instance Methods

+(duration) click to toggle source
# File lib/date_period/month.rb, line 47
def +(duration)
  self.class.including(first_day + Duration(duration))
end
-(duration) click to toggle source
# File lib/date_period/month.rb, line 51
def -(duration)
  self.class.including(first_day - Duration(duration))
end
<=>(other) click to toggle source
# File lib/date_period/month.rb, line 43
def <=>(other)
  [year, month] <=> [other.year, other.month]
end
date_period() click to toggle source
# File lib/date_period/month.rb, line 55
def date_period
  first_day .. ((self + 1).first_day - 1.day)
end
eql?(other) click to toggle source
# File lib/date_period/month.rb, line 63
def eql?(other)
  hash == other.hash
end
first_day() click to toggle source
# File lib/date_period/month.rb, line 67
def first_day
  Date.new(year, month, 1)
end
first_moment(zone: Time.zone) click to toggle source
# File lib/date_period/month.rb, line 79
def first_moment(zone: Time.zone)
  Time.use_zone(zone){ first_day.beginning_of_day }
end
include?(datish) click to toggle source
# File lib/date_period/month.rb, line 91
def include?(datish)
  datish.year == year && datish.month == month
end
inspect() click to toggle source
# File lib/date_period/month.rb, line 95
def inspect
  first_day.strftime "%b %Y"
end
last_day() click to toggle source
# File lib/date_period/month.rb, line 71
def last_day
  first_day.end_of_month
end
last_moment(zone: Time.zone) click to toggle source
# File lib/date_period/month.rb, line 83
def last_moment(zone: Time.zone)
  Time.use_zone(zone){ last_day.end_of_day }
end
next()
Alias for: succ
prev() click to toggle source
# File lib/date_period/month.rb, line 39
def prev
  self.class.new year: year, month: month - 1
end
succ() click to toggle source
# File lib/date_period/month.rb, line 34
def succ
  self.class.new year: year, month: month + 1
end
Also aliased as: next
time_period() click to toggle source
# File lib/date_period/month.rb, line 87
def time_period
  first_moment .. last_moment
end
to_date() click to toggle source
# File lib/date_period/month.rb, line 59
def to_date
  first_day
end

Protected Instance Methods

hash() click to toggle source
# File lib/date_period/month.rb, line 101
def hash
  @hash ||= [self.class, year, month].hash
end

Private Instance Methods

Duration(duration) click to toggle source
# File lib/date_period/month.rb, line 107
def Duration(duration)
  duration.kind_of?(ActiveSupport::Duration) ? duration : duration.month
end