class Month

Constants

ABBREVIATIONS
NAMES
REGEXP1
REGEXP2
REGEXP3

Attributes

month[R]
number[R]
year[R]

Public Class Methods

new(year, number) click to toggle source
# File lib/month.rb, line 20
def initialize(year, number)
  unless NAMES.key?(number)
    raise ArgumentError, 'invalid month number'
  end

  @year, @number = year, number

  freeze
end
now() click to toggle source
# File lib/month.rb, line 213
def Month.now
  Month(Time.now)
end
parse(string) click to toggle source
# File lib/month.rb, line 188
def self.parse(string)
  case string
  when REGEXP1
    Month.new($1.to_i, $2.to_i)
  when REGEXP2
    Month.new($1.to_i, ABBREVIATIONS.fetch($2))
  when REGEXP3
    Month.new($2.to_i, Date::MONTHNAMES.index($1))
  else
    raise ArgumentError, 'invalid month'
  end
end

Public Instance Methods

+(number) click to toggle source
# File lib/month.rb, line 126
def +(number)
  a, b = (@number - 1 + number).divmod(12)

  self.class.new(@year + a, b + 1)
end
-(object) click to toggle source
# File lib/month.rb, line 132
def -(object)
  if object.is_a?(Integer)
    self + (-object)
  else
    12 * (year - object.year) + (@number - object.number)
  end
end
<<(n) click to toggle source
# File lib/month.rb, line 90
def <<(n)
  self - n
end
<=>(month) click to toggle source
# File lib/month.rb, line 58
def <=>(month)
  return unless month.class == self.class

  if @year == month.year
    @number <=> month.number
  else
    @year <=> month.year
  end
end
===(time)
Alias for: include?
>>(n) click to toggle source
# File lib/month.rb, line 86
def >>(n)
  self + n
end
dates() click to toggle source
# File lib/month.rb, line 154
def dates
  start_date .. end_date
end
downto(min, &block) click to toggle source
# File lib/month.rb, line 122
def downto(min, &block)
  step(min, -1, &block)
end
encode_with(coder) click to toggle source
# File lib/month/yaml.rb, line 4
def encode_with(coder)
  coder.represent_scalar(nil, to_s)
end
end_date() click to toggle source
# File lib/month.rb, line 150
def end_date
  Date.new(@year, @number, -1)
end
eql?(object) click to toggle source
# File lib/month.rb, line 54
def eql?(object)
  object.class == self.class && object.hash == self.hash
end
hash() click to toggle source
# File lib/month.rb, line 50
def hash
  [@year, @number].hash
end
include?(time) click to toggle source
# File lib/month.rb, line 140
def include?(time)
  @year == time.year && @number == time.month
end
Also aliased as: ===
iso8601()
Alias for: to_s
length() click to toggle source
# File lib/month.rb, line 158
def length
  end_date.mday
end
name() click to toggle source
# File lib/month.rb, line 40
def name
  NAMES.fetch(@number)
end
next() click to toggle source
# File lib/month.rb, line 70
def next
  if @number == 12
    self.class.new(@year + 1, 1)
  else
    self.class.new(@year, @number + 1)
  end
end
Also aliased as: succ, next_month
next_month()
Alias for: next
prev_month() click to toggle source
# File lib/month.rb, line 82
def prev_month
  self - 1
end
start_date() click to toggle source
# File lib/month.rb, line 146
def start_date
  Date.new(@year, @number, 1)
end
step(limit, step = 1) { |month| ... } click to toggle source
# File lib/month.rb, line 94
def step(limit, step = 1)
  raise ArgumentError if step.zero?

  unless block_given?
    return enum_for(:step, limit, step)
  end

  month = self

  if step > 0
    until month > limit
      yield month

      month += step
    end
  else
    until month < limit
      yield month

      month += step
    end
  end
end
succ()
Alias for: next
to_s() click to toggle source
# File lib/month.rb, line 34
def to_s
  "#{@year}-#{@number.to_s.rjust(2, '0')}"
end
Also aliased as: iso8601
upto(max, &block) click to toggle source
# File lib/month.rb, line 118
def upto(max, &block)
  step(max, 1, &block)
end