class Quarter

Constants

ISO8601_REGEXP
REGEXP

Attributes

number[R]
year[R]

Public Class Methods

new(year, number) click to toggle source
# File lib/quarter.rb, line 5
def initialize(year, number)
  unless number.between?(1, 4)
    raise ArgumentError, 'invalid quarter number'
  end

  @year = year

  @number = number

  freeze
end
now() click to toggle source
# File lib/quarter.rb, line 172
def Quarter.now
  Quarter(Time.now)
end
parse(string) click to toggle source
# File lib/quarter.rb, line 156
def self.parse(string)
  case string
  when REGEXP
    Quarter.new($2.to_i, $1.to_i)
  when ISO8601_REGEXP
    Quarter.new($1.to_i, $2.to_i)
  else
    raise ArgumentError, 'invalid quarter string'
  end
end
today() click to toggle source
# File lib/quarter.rb, line 168
def Quarter.today
  Quarter(Date.today)
end

Public Instance Methods

+(other) click to toggle source
# File lib/quarter.rb, line 91
def +(other)
  years, remainder = (@number - 1 + other.to_i).divmod(4)

  self.class.new(@year + years, 1 + remainder)
end
-(other) click to toggle source
# File lib/quarter.rb, line 97
def -(other)
  if other.class == self.class
    (@year * 4 + @number) - (other.year * 4 + other.number)
  else
    self + (-other)
  end
end
<=>(other) click to toggle source
# File lib/quarter.rb, line 41
def <=>(other)
  return unless other.class == self.class

  if @year == other.year
    @number <=> other.number
  else
    @year <=> other.year
  end
end
===(date)
Alias for: include?
dates() click to toggle source
# File lib/quarter.rb, line 119
def dates
  start_date .. end_date
end
downto(min, &block) click to toggle source
# File lib/quarter.rb, line 87
def downto(min, &block)
  step(min, -1, &block)
end
encode_with(coder) click to toggle source
# File lib/quarter/yaml.rb, line 4
def encode_with(coder)
  coder.represent_scalar(nil, to_s)
end
end_date() click to toggle source
# File lib/quarter.rb, line 115
def end_date
  Date.new(@year, @number * 3, -1)
end
eql?(other) click to toggle source
# File lib/quarter.rb, line 37
def eql?(other)
  other.class == self.class && other.hash == self.hash
end
hash() click to toggle source
# File lib/quarter.rb, line 33
def hash
  iso8601.hash
end
include?(date) click to toggle source
# File lib/quarter.rb, line 105
def include?(date)
  @year == date.year && @number == Quarter(date.month)
end
Also aliased as: ===
iso8601() click to toggle source
# File lib/quarter.rb, line 29
def iso8601
  "#{@year}-Q#{@number}"
end
length() click to toggle source
# File lib/quarter.rb, line 123
def length
  case @number
  when 1 then Date.gregorian_leap?(@year) ? 91 : 90
  when 2 then 91
  when 3, 4 then 92
  end
end
months() click to toggle source
# File lib/quarter.rb, line 131
def months
  Month.new(@year, @number) .. Month.new(@year, @number + 2)
end
name() click to toggle source
# File lib/quarter.rb, line 21
def name
  "Q#{@number}"
end
next() click to toggle source
# File lib/quarter.rb, line 53
def next
  self + 1
end
Also aliased as: succ, next_quarter
next_quarter()
Alias for: next
prev() click to toggle source
# File lib/quarter.rb, line 61
def prev
  self - 1
end
Also aliased as: prev_quarter
prev_quarter()
Alias for: prev
start_date() click to toggle source
# File lib/quarter.rb, line 111
def start_date
  Date.new(@year, @number * 3 - 2, 1)
end
step(limit, step = 1) { |quarter| ... } click to toggle source
# File lib/quarter.rb, line 67
def step(limit, step = 1)
  raise ArgumentError if step.zero?

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

  quarter = self

  operator = step > 0 ? :> : :<

  until quarter.public_send(operator, limit)
    yield quarter

    quarter += step
  end
end
succ()
Alias for: next
to_s() click to toggle source
# File lib/quarter.rb, line 25
def to_s
  "Q#{@number} #{@year}"
end
upto(max, &block) click to toggle source
# File lib/quarter.rb, line 83
def upto(max, &block)
  step(max, 1, &block)
end