module CalendarChina::Methods

Public Instance Methods

is_holiday?(date = china_date) click to toggle source
# File lib/calendar_china/methods.rb, line 3
def is_holiday?(date = china_date)
  holidays.include?(date)
end
is_rest?(date = china_date) click to toggle source
# File lib/calendar_china/methods.rb, line 11
def is_rest?(date = china_date)
  !is_workday?(date)
end
is_workday?(date = china_date) click to toggle source
# File lib/calendar_china/methods.rb, line 7
def is_workday?(date = china_date)
  special_work_days.include?(date) || !(is_holiday?(date) || date.to_date.on_weekend?)
end
next_workday(date = china_date) click to toggle source
# File lib/calendar_china/methods.rb, line 15
def next_workday(date = china_date)
  result = {}
  st = date.to_date.deep_dup
  current_date = date.to_date
  loop do
    next_date = current_date.next
    date_str = format_date(next_date)
    if is_workday?(date_str)
      days = (next_date - st).to_i
      result[:date] = date_str
      result[:days] = days - 1
      break
    else
      current_date = next_date
    end
  end
  result
end

Private Instance Methods

holidays() click to toggle source
# File lib/calendar_china/methods.rb, line 44
def holidays
  @holidays ||= metadata.map do |data|
    data['date'] if data['isOffDay']
  end.compact
end
metadata() click to toggle source
# File lib/calendar_china/methods.rb, line 36
def metadata
  return @metadata if @metadata

  path = File.expand_path("../../data/#{china_year}.json", __FILE__)
  @metadata = JSON.parse(File.read(path))['days']
  @metadata
end
special_work_days() click to toggle source
# File lib/calendar_china/methods.rb, line 50
def special_work_days
  @special_work_days ||= metadata.map do |data|
    data['date'] unless data['isOffDay']
  end.compact
end