class Schedule

Schedule for CAMPHOR- HOUSE assignments and other events

Constants

ICAL_PRIVATE_PATH
ICAL_PUBLIC_PATH
JSON_PRIVATE_PATH
JSON_PUBLIC_PATH
JSON_SCHEMA_PATH

Public Class Methods

initialize_for_month(year, month) click to toggle source

指定された月の初期割り当てを @events に設定する

# File lib/hanreki/schedule.rb, line 25
def self.initialize_for_month(year, month)
  schedule = Schedule.new
  schedule.send(:initialize_assignments, year, month)
  schedule
end
new() click to toggle source
# File lib/hanreki/schedule.rb, line 18
def initialize
  # マスターファイルを生成する際のファイル名の決定に使用
  @first_day_of_month = nil
  @events = []
end

Public Instance Methods

load_master_files() click to toggle source

マスターファイルを読み込んで @events に設定する

# File lib/hanreki/schedule.rb, line 32
def load_master_files
  @events = Dir.glob('master/*.csv').flat_map do |f|
    month = f.match(/master\/(\d+).csv/)[1]
    CSV.open(f, headers: true) do |csv|
      csv.map { |row| Event.from_master(f, $., month, row) }
    end
  end
end
master_file_exists?() click to toggle source
# File lib/hanreki/schedule.rb, line 75
def master_file_exists?
  File.exists? file_path
end
out_ical() click to toggle source

Output private and public iCal calendar files

# File lib/hanreki/schedule.rb, line 42
def out_ical
  File.open(ICAL_PUBLIC_PATH, 'w') do |f|
    ical = ICalendar.new
    public_events.each { |event| ical.set_event(event, :public) }
    f.write(ical)
  end
  File.open(ICAL_PRIVATE_PATH, 'w') do |f|
    ical = ICalendar.new
    private_events.each { |event| ical.set_event(event, :private) }
    f.write(ical)
  end
end
out_json() click to toggle source

Output private and public JSON calendar files

# File lib/hanreki/schedule.rb, line 56
def out_json
  File.open(JSON_PUBLIC_PATH, 'w') do |f|
    f.write(events_to_json(public_events, :public, validate: true))
  end
  File.open(JSON_PRIVATE_PATH, 'w') do |f|
    f.write(events_to_json(private_events, :private, validate: true))
  end
end
out_master() click to toggle source

Output master file

# File lib/hanreki/schedule.rb, line 66
def out_master
  CSV.open(
    file_path, 'w',
    headers: Event.master_header, write_headers: true
  ) do |master_file|
    @events.each { |event| master_file << event.to_master }
  end
end
sort_by_date!() click to toggle source
# File lib/hanreki/schedule.rb, line 79
def sort_by_date!
  @events.sort_by! { |e| [e.start, e.end] }
end

Private Instance Methods

default_assignment(date) click to toggle source
# File lib/hanreki/schedule.rb, line 101
def default_assignment(date)
  # Set timezone explicitly (DO NOT use date.to_time, it uses local timezone)
  time = Time.new(date.year, date.month, date.day, 0, 0, 0, '+09:00')
  Event.new(
    start: time,
    end: time.next_day(1),
    public_summary: '',
    private_summary: 'Closed')
end
events_to_json(events, type, validate = false) click to toggle source
# File lib/hanreki/schedule.rb, line 119
def events_to_json(events, type, validate = false)
  json = events.map { |event| event.to_h(type, :string) }.to_json
  validate_json!(json) if validate
  json
end
file_path() click to toggle source
# File lib/hanreki/schedule.rb, line 85
def file_path
  "master/#{@first_day_of_month.strftime('%Y%m')}.csv".freeze
end
initialize_assignments(year, month) click to toggle source
# File lib/hanreki/schedule.rb, line 89
def initialize_assignments(year, month)
  @first_day_of_month =
    begin
      Date.new(year, month, 1)
    rescue TypeError
      raise ArgumentError, 'Invalid year and month'
    end

  days = @first_day_of_month...@first_day_of_month.next_month
  @events = days.map { |day| default_assignment(day) }
end
private_events() click to toggle source
# File lib/hanreki/schedule.rb, line 115
def private_events
  @events.select(&:private?)
end
public_events() click to toggle source
# File lib/hanreki/schedule.rb, line 111
def public_events
  @events.select(&:public?)
end
validate_json!(json) click to toggle source
# File lib/hanreki/schedule.rb, line 125
def validate_json!(json)
  JSON::Validator.validate!(JSON_SCHEMA_PATH, json)
end