class Course
Public Class Methods
by_key(course_key)
click to toggle source
# File lib/buweb/course.rb, line 74 def self.by_key(course_key) where(parse_key(course_key)).first end
by_keys(*course_keys)
click to toggle source
# File lib/buweb/course.rb, line 78 def self.by_keys(*course_keys) course_keys = course_keys.flatten if course_keys.present? any_of(*course_keys.map { |key| parse_key(key) }) else none end end
parse_key(course_key)
click to toggle source
# File lib/buweb/course.rb, line 69 def self.parse_key(course_key) match = course_key.to_s.match(/(?<prefix>[a-z]+)\s?(?<code>[0-9]+)/i) {prefix: match.try(:[], :prefix), code: match.try(:[], :code).try(:to_i)} end
update_from_catalog_entry(entry_hash)
click to toggle source
TODO: These catalog update methods should probably be moved into their own class
# File lib/buweb/course.rb, line 89 def self.update_from_catalog_entry(entry_hash) # find or initialize by prefix and code; ie. BBST 103 Catalog.update_from_catalog_entry(entry_hash) Course.find_or_initialize_by(prefix: entry_hash["Prefix"], code: entry_hash["Code"]).tap do |c| if c.new_record? entry = c.catalog_entries.new raw_data: entry_hash, external_id: entry_hash["Course OID"], external_catalog_id: entry_hash["Catalog OID"] c.update_from_entry(entry) else # does this catalog entry already exist? match on "Catalog OID" and "Course OID" entry = c.catalog_entries.find_or_initialize_by(external_catalog_id: entry_hash["Catalog OID"], external_id: entry_hash["Course OID"]) entry.raw_data = entry_hash c.update_from_entry(entry) end end end
Public Instance Methods
course_key()
click to toggle source
# File lib/buweb/course.rb, line 57 def course_key "#{subject} #{code}" end
Also aliased as: course_key_string
course_key_string=(course_key)
click to toggle source
# File lib/buweb/course.rb, line 63 def course_key_string=(course_key) match = Course.parse_key(course_key) self.prefix = match[:prefix] self.code = match[:code] end
to_s()
click to toggle source
# File lib/buweb/course.rb, line 133 def to_s name end
update_from_entry(entry)
click to toggle source
# File lib/buweb/course.rb, line 111 def update_from_entry(entry) self.prefix = entry.raw_data["Prefix"] self.code = entry.raw_data["Code"] self.name = entry.raw_data["Name"] self.description = entry.raw_data["Description (Rendered)"] self.when_offered = entry.raw_data["When Offered"] self.lecture_lab_hours = entry.raw_data["Lecture/Lab Hours"] self.cross_listed_course_keys = entry.raw_data["Cross-listed"].try(:gsub, /[.]{1}$/, '') self.prerequisites = entry.raw_data["Prerequisite(s)"].try(:gsub, /[.]{1}$/, '') self.corequisites = entry.raw_data["Corequisite(s)"].try(:gsub, /[.]{1}$/, '') self.restrictions = entry.raw_data["Restriction(s)"] self.catalog_notes = entry.raw_data["Note(s)"] self.fee = entry.raw_data["Fee"].try(:gsub, /[.]{1}$/, '') self.grade_mode = entry.raw_data["Grade Mode"].try(:gsub, /[.]{1}$/, '') self.credits = entry.raw_data["Credit(s)"].try(:gsub, /[.]{1}$/, '') if entry.raw_data["Prefix"] && entry.raw_data["Code"] && entry.raw_data["Name"] self.save(validate: false) else raise "Course must contain a prefix, code, and name." end end
update_from_latest_entry()
click to toggle source
# File lib/buweb/course.rb, line 106 def update_from_latest_entry entry = catalog_entries.order_by(:last_modified_at.desc).first # Not sure this will exist yet to query update_from_entry(entry) end