class FinnishHolidays::Calendar

Constants

MAX_HOLIDAYS

Public Class Methods

new(y = nil, m = nil, d = nil) click to toggle source
# File lib/finnish-holidays/classes/calendar.rb, line 9
def initialize(y = nil, m = nil, d = nil)
  @y = y == nil ? Time.now.year : y
  @m = m == nil ? Time.now.month : m
  @d = d == nil ? Time.now.day : d

  @year = FinnishHolidays::Year.new(@y)
end

Public Instance Methods

month(month, year, weekends = false) click to toggle source
# File lib/finnish-holidays/classes/calendar.rb, line 61
def month(month, year, weekends = false)
  if !weekends
    @year.discard_weekends()
  end

  holidays = []

  if @year.holidays[month].is_a? Array
    @year.holidays[month].each do |holiday|
      holidays.push(holiday)
    end
  end

  holidays
end
next(count = 3, include_weekends = false) click to toggle source
# File lib/finnish-holidays/classes/calendar.rb, line 17
def next(count = 3, include_weekends = false)
  if count > MAX_HOLIDAYS
    raise "Cannot request more than #{MAX_HOLIDAYS} holidays at once."
  end

  holidays = []

  while holidays.length < count
    if !include_weekends
      @year.discard_weekends()
    end

    if (defined? @year.holidays[@m]) && (@year.holidays[@m].is_a? Array)
      @year.holidays[@m].each do |holiday|
        if (holidays.length < count) && (holiday['day'] >= @d)
          holidays.push(holiday)
        end
      end
    end

    if holidays.length < count
      next_month()
    end
  end

  holidays
end
year(year, weekends = false) click to toggle source
# File lib/finnish-holidays/classes/calendar.rb, line 45
def year(year, weekends = false)
  if !weekends
    @year.discard_weekends()
  end

  holidays = []

  @year.holidays.each do |month, array|
    array.each do |holiday|
      holidays.push(holiday)
    end
  end

  holidays
end

Private Instance Methods

next_month() click to toggle source
# File lib/finnish-holidays/classes/calendar.rb, line 79
def next_month
  if @m == 12
    @m = 1
    @y += 1
    @d = 1
  else
    @m += 1
    @d = 1
  end

  @year = FinnishHolidays::Year.new(@y)
end