Skip to content

Commit

Permalink
Refactor paritiy ENUMs
Browse files Browse the repository at this point in the history
  • Loading branch information
tszolar committed Sep 21, 2016
1 parent ef0ac78 commit 004734e
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 129 deletions.
10 changes: 0 additions & 10 deletions app/models/faculty_semester.rb
@@ -1,5 +1,4 @@
require 'date_refinements'
require 'parity'
require 'models/semester_period'

class FacultySemester < Sequel::Model
Expand Down Expand Up @@ -42,13 +41,4 @@ def self.find_by_date_range_with_periods(start_date, end_date, faculty_id)
.eager(semester_periods: ->(ds) { ds.where('starts_at < ?', end_date.end_of_week) })
.all # <- this is necessary for eager to work correctly!
end

def first_week_parity
Parity.from_numeric(super)
end

def first_week_parity=(new_parity)
super Parity.to_numeric(new_parity)
end

end
19 changes: 3 additions & 16 deletions app/models/semester_period.rb
@@ -1,5 +1,4 @@
require 'sirius/enums/semester_period_type'
require 'parity'
require 'day'
require 'date_refinements'

Expand All @@ -20,18 +19,6 @@ def teaching?
type == :teaching
end

def first_week_parity
Parity.from_numeric(super) if super
end

def first_week_parity=(new_parity)
if new_parity
super Parity.to_numeric(new_parity)
else
super
end
end

def first_day_override
Day.from_numeric(super) if super
end
Expand Down Expand Up @@ -71,7 +58,7 @@ def include?(date)
# and the given date, shifted by the period's `first_week_parity`.
#
# @param date [Date]
# @return [Symbol, nil] `:even`, `:odd`, or `nil`
# @return [String, nil] `'even'`, `'odd'`, or `nil`
# @raise [ArgumentError] if the date's week is not within this period.
#
def week_parity(date)
Expand All @@ -82,10 +69,10 @@ def week_parity(date)
end

weeks_since_start = ((date.start_of_week - starts_at.start_of_week) / 7).abs.floor
first_parity = first_week_parity == :even ? 0 : 1
first_parity = first_week_parity == 'even' ? 0 : 1
parity = (weeks_since_start + first_parity) % 2

parity == 0 ? :even : :odd
parity == 0 ? 'even' : 'odd'
end

alias_method :irregular?, :irregular
Expand Down
9 changes: 0 additions & 9 deletions app/models/timetable_slot.rb
@@ -1,20 +1,11 @@
require 'day'
require 'parity'
require 'models/room'

class TimetableSlot < Sequel::Model

many_to_one :parallel
many_to_one :room

def parity
Parity.from_numeric(super)
end

def parity=(new_parity)
super Parity.to_numeric(new_parity)
end

def day
Day.from_numeric(super)
end
Expand Down
2 changes: 1 addition & 1 deletion app/roles/planned_semester_period.rb
Expand Up @@ -34,7 +34,7 @@ def combine_date_with_time(date, time)
end

def schedule_start_day(teaching_time_parity)
if teaching_time_parity == :both || teaching_time_parity == first_week_parity
if teaching_time_parity == 'both' || teaching_time_parity == first_week_parity
starts_at
else
starts_at.next_week(:monday)
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/1469470701_convert_parities_to_enum.rb
@@ -0,0 +1,17 @@
Sequel.migration do
up do
extension :pg_enum
create_enum :parity, %w(both odd even)
set_column_type :faculty_semesters, :first_week_parity, :parity, using: '(ENUM_RANGE(NULL::parity))[first_week_parity + 1]'
set_column_type :semester_periods, :first_week_parity, :parity, using: '(ENUM_RANGE(NULL::parity))[first_week_parity + 1]'
set_column_type :timetable_slots, :parity, :parity, using: '(ENUM_RANGE(NULL::parity))[parity + 1]'
end

down do
extension :pg_enum
set_column_type :faculty_semesters, :first_week_parity, :integer, using: '(array_position(ENUM_RANGE(NULL::parity), first_week_parity) - 1)'
set_column_type :semester_periods, :first_week_parity, :integer, using: '(array_position(ENUM_RANGE(NULL::parity), first_week_parity) - 1)'
set_column_type :timetable_slots, :parity, :integer, using: '(array_position(ENUM_RANGE(NULL::parity), parity) - 1)'
drop_enum :parity
end
end
28 changes: 14 additions & 14 deletions db/seeds.rb
Expand Up @@ -14,7 +14,7 @@

FacultySemester.find_or_create(code: 'B141', faculty: 18000) do |s|
s.update_parallels = false
s.first_week_parity = :odd
s.first_week_parity = 'odd'
s.starts_at = '2014-09-22'
s.teaching_ends_at = '2014-12-20'
s.exams_start_at = '2015-01-05'
Expand All @@ -26,7 +26,7 @@

FacultySemester.find_or_create(code: 'B141', faculty: 13000) do |s|
s.update_parallels = false
s.first_week_parity = :odd
s.first_week_parity = 'odd'
s.starts_at = '2014-09-22'
s.teaching_ends_at = '2014-12-20'
s.exams_start_at = '2015-01-05'
Expand All @@ -39,7 +39,7 @@
fitb142 = FacultySemester.find_or_create(code: 'B142', faculty: 18000) do |s|
s.update_parallels = false
s.update_other = true
s.first_week_parity = :even
s.first_week_parity = 'even'
s.starts_at = '2015-02-16'
s.teaching_ends_at = '2015-05-16'
s.exams_start_at = '2015-05-18'
Expand All @@ -52,7 +52,7 @@
fitb151 = FacultySemester.find_or_create(code: 'B151', faculty: 18000) do |s|
s.update_parallels = true
s.update_other = true
s.first_week_parity = :odd
s.first_week_parity = 'odd'
s.starts_at = '2015-10-05' # FIXME: actual start is on 01
s.teaching_ends_at = '2016-01-10'
s.exams_start_at = '2016-01-11'
Expand All @@ -65,7 +65,7 @@
felb151 = FacultySemester.find_or_create(code: 'B151', faculty: 13000) do |s|
s.update_parallels = true
s.update_other = true
s.first_week_parity = :even
s.first_week_parity = 'even'
s.starts_at = '2015-10-01'
s.teaching_ends_at = '2016-01-17'
s.exams_start_at = '2016-01-18'
Expand All @@ -80,7 +80,7 @@
type: :teaching,
starts_at: '2015-02-16',
ends_at: '2015-05-15',
first_week_parity: :even,
first_week_parity: 'even',
faculty_semester: fitb142,
},
{
Expand All @@ -101,22 +101,22 @@
type: :teaching,
starts_at: '2015-10-05',
ends_at: '2015-12-20',
first_week_parity: :odd,
first_week_parity: 'odd',
faculty_semester: fitb151,
},
{
type: :teaching,
starts_at: '2015-12-21',
ends_at: '2015-12-21',
first_week_parity: :even,
first_week_parity: 'even',
first_day_override: :wednesday,
faculty_semester: fitb151,
},
{
type: :teaching,
starts_at: '2015-12-22',
ends_at: '2015-12-22',
first_week_parity: :odd,
first_week_parity: 'odd',
first_day_override: :tuesday,
faculty_semester: fitb151,
},
Expand All @@ -130,7 +130,7 @@
type: :teaching,
starts_at: '2016-01-04',
ends_at: '2016-01-10',
first_week_parity: :even,
first_week_parity: 'even',
faculty_semester: fitb151,
},
{
Expand All @@ -144,7 +144,7 @@
type: :teaching,
starts_at: '2015-10-01',
ends_at: '2015-12-22',
first_week_parity: :odd,
first_week_parity: 'odd',
faculty_semester: felb151,
},
{
Expand All @@ -157,22 +157,22 @@
type: :teaching,
starts_at: '2016-01-04',
ends_at: '2016-01-10',
first_week_parity: :even,
first_week_parity: 'even',
faculty_semester: felb151,
},
{
type: :teaching,
starts_at: '2016-01-11',
ends_at: '2016-01-11',
first_week_parity: :odd,
first_week_parity: 'odd',
first_day_override: :wednesday,
faculty_semester: felb151,
},
{
type: :teaching,
starts_at: '2016-01-12',
ends_at: '2016-01-17',
first_week_parity: :odd,
first_week_parity: 'odd',
faculty_semester: felb151,
},
{
Expand Down
1 change: 1 addition & 0 deletions lib/actors/teacher_timetable_slot_transformer.rb
Expand Up @@ -52,6 +52,7 @@ def plan_events(slot, teacher)
unless slot.duration
slot.duration = 2
end
slot.parity = slot.parity.to_s
events = periods.flat_map do |period|
PlannedTimetableSlot.new(slot, time_converter).generate_events(@semester, period)
end
Expand Down
16 changes: 0 additions & 16 deletions lib/parity.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/sirius/teaching_time.rb
Expand Up @@ -38,7 +38,7 @@ def to_recurrence_rule(day_offset, schedule_ends_at)
end

def week_frequency
if parity == :both
if parity == 'both'
1
else
2
Expand Down
2 changes: 1 addition & 1 deletion lib/sirius_api/semester_day.rb
Expand Up @@ -64,7 +64,7 @@ def wday_name
# Unlike the same named method in {SemesterWeek}, this one considers even
# irregular periods!
#
# @return [Symbol, nil] `:even`, `:odd`, or `nil`
# @return [String, nil] `'even'`, `'odd'`, or `nil`
# @see SemesterPeriod#week_parity
#
def week_parity
Expand Down
2 changes: 1 addition & 1 deletion lib/sirius_api/semester_week.rb
Expand Up @@ -144,7 +144,7 @@ def teaching?
# The calculation is based on difference between the period's start date and
# and this week's date, shifted by the period's `first_week_parity`.
#
# @return [Symbol, nil] `:even`, `:odd`, or `nil`
# @return [String, nil] `'even'`, `'odd'`, or `nil`
#
def week_parity
return @week_parity if defined? @week_parity
Expand Down
2 changes: 1 addition & 1 deletion spec/fabricators/faculty_semester_fabricator.rb
Expand Up @@ -6,7 +6,7 @@
faculty 18000
update_parallels true
update_other true
first_week_parity :odd
first_week_parity 'odd'
starts_at '2014-09-22'
teaching_ends_at '2014-12-20'
exams_start_at '2015-01-05'
Expand Down
4 changes: 2 additions & 2 deletions spec/fabricators/semester_period_fabricator.rb
Expand Up @@ -5,14 +5,14 @@
faculty_semester
type :teaching
irregular false
first_week_parity :odd
first_week_parity 'odd'
starts_at '2014-09-22'
ends_at '2015-02-14'
end

Fabricator(:teaching_semester_period, from: :semester_period) do
type :teaching
first_week_parity :odd
first_week_parity 'odd'
starts_at '2014-09-22'
ends_at '2014-12-20'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fabricators/timetable_slot_fabricator.rb
Expand Up @@ -2,7 +2,7 @@

Fabricator(:timetable_slot) do
day Day::MONDAY
parity :both
parity 'both'
first_hour 1
duration 2
room
Expand Down
6 changes: 3 additions & 3 deletions spec/interactors/convert_tts_spec.rb
Expand Up @@ -17,7 +17,7 @@

context 'with slots' do

let(:slot) { double(id: 239019, to_hash: {day: 5, duration: 2, parity: :both, first_hour: 3}, day: 5, room: double(link_id: 'MK:209')) }
let(:slot) { double(id: 239019, to_hash: {day: 5, duration: 2, parity: 'both', first_hour: 3}, day: 5, room: double(link_id: 'MK:209')) }
let(:room) { Fabricate(:room, id: 'MK:209') }
let(:slots) { {'1234' => [slot]} }

Expand All @@ -27,7 +27,7 @@
expect(converted_slot.id).to eq 239019
expect(converted_slot.day).to eq :friday
expect(converted_slot.duration).to eq 2
expect(converted_slot.parity).to eq :both
expect(converted_slot.parity).to eq 'both'
expect(converted_slot.first_hour).to eq 3
end

Expand All @@ -47,7 +47,7 @@

context 'with invalid slots' do

let(:slot) { double(id: 239019, to_hash: {duration: 2, parity: :both, first_hour: 3}, day: nil, room: double(link_id: 'MK:209')) } # missing day
let(:slot) { double(id: 239019, to_hash: {duration: 2, parity: 'both', first_hour: 3}, day: nil, room: double(link_id: 'MK:209')) } # missing day
let(:slots) { {'1234' => [slot]} }

it 'rejects them' do
Expand Down
24 changes: 0 additions & 24 deletions spec/lib/parity_spec.rb

This file was deleted.

8 changes: 4 additions & 4 deletions spec/lib/sirius/teaching_time_spec.rb
Expand Up @@ -10,14 +10,14 @@
described_class.new(teaching_period: Period.parse('14:30', '16:00'),
parity: parity, day: input_day)
end
let(:parity) { :odd }
let(:parity) { 'odd' }
let(:input_day) { :tuesday }

describe '#week_frequency' do
where :parity, :week_frequency do
:odd | 2
:even | 2
:both | 1
'odd' | 2
'even' | 2
'both' | 1
end
with_them ->{ "with #{parity} parity" } do
it 'returns correct frequency' do
Expand Down

0 comments on commit 004734e

Please sign in to comment.