class MonthYear

Constants

VERSION

Attributes

month[R]
year[R]

Public Class Methods

dump(month_year) click to toggle source
# File lib/month_year.rb, line 31
def dump(month_year)
  raise_dump_error(month_year) unless month_year.is_a?(self)
  month_year.to_i
end
from_date(date) click to toggle source

Instantiate a `MonthYear` object from a `Date`-like object (a `Date` or `Time` instance, or in fact from any object responding to :year and :month).

Example:

>> MonthYear.from_date(Date.today)
=> #<MonthYear:0x00000001f15c10 @month=12, @year=2016>

Arguments:

date: (Date)
# File lib/month_year.rb, line 19
def from_date(date)
  raise ArgumentError unless [:year, :month].all? {|v| date.respond_to?(v) }
  self.new(date.year, date.month)
end
load(month_year) click to toggle source
# File lib/month_year.rb, line 24
def load(month_year)
  raise_load_error(month_year) unless month_year.is_a?(Integer)
  year = month_year / 100
  month = month_year % 100
  self.new(year, month)
end
new(year, month) click to toggle source

Instantiate a new `MonthYear` object.

Example:

>> MonthYear.new(2016, 12)
=> #<MonthYear:0x00000001f15c10 @month=12, @year=2016>

Arguments:

year: (Integer)
month: (Integer)
# File lib/month_year.rb, line 47
def initialize(year, month)
  raise ArgumentError unless [year, month].all? {|v| Fixnum === v }
  raise ArgumentError unless (1..12).cover?(month)
  @year, @month = year, month
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/month_year.rb, line 58
def <=>(other)
  (year <=> other.year).nonzero? || month <=> other.month
end
==(other) click to toggle source
# File lib/month_year.rb, line 53
def ==(other)
  other.class == self.class && (self <=> other) == 0
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/month_year.rb, line 62
def hash
  to_i.hash
end
next()
Alias for: succ
succ() click to toggle source
# File lib/month_year.rb, line 66
def succ
  if month == 12
    self.class.new(year+1, 1)
  else
    self.class.new(year, month+1)
  end
end
Also aliased as: next
to_i() click to toggle source

Return the numeric representation of this `MonthYear` instance.

Example:

>> MonthYear.new(2016, 12).to_i
=> 201612
# File lib/month_year.rb, line 81
def to_i
  year * 100 + month
end
to_s() click to toggle source

Return the string representation of this `MonthYear` instance.

Example:

>> MonthYear.new(2016, 12).to_s
=> "2016-12"
# File lib/month_year.rb, line 91
def to_s
  "#{year}-#{month}"
end

Protected Instance Methods

state() click to toggle source
# File lib/month_year.rb, line 97
def state
  [year, month]
end

Private Instance Methods

active_record?() click to toggle source
# File lib/month_year.rb, line 103
def active_record?
  Object.const_defined?(::ActiveRecord::SerializationTypeMismatch)
end
argument_error_class() click to toggle source
# File lib/month_year.rb, line 107
def argument_error_class
  active_record? ? ::ActiveRecord::SerializationTypeMismatch : ArgumentError
end
throw_dump_error(obj) click to toggle source
# File lib/month_year.rb, line 115
def throw_dump_error(obj)
  raise argument_error_class, "Argument was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}"
end
throw_load_error(obj) click to toggle source
# File lib/month_year.rb, line 111
def throw_load_error(obj)
  raise argument_error_class, "Argument was supposed to be an Integer, but was a #{obj.class}. -- #{obj.inspect}"
end