Sha256: 365c4990f3c2069bed4da3cf542e8b4620c07539299a0f98c8d4455f9ef09818

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'tree'

class Lectures < Section
	def initialize sect, citems, schedule=nil
		super sect, citems
		@schedule = schedule || ::Scheduler.new
		@citems = sort_items
		build_tree
	end

	def has_subsections?
		true
	end

	def has_lecture_numbers?
		true
	end

	def build_tree
		lecture_num = 1
		@root = Tree::TreeNode.new("root", "root")
		@citems.each do |i|
			i.lecture_number = lecture_num
			i.lecture_date = @schedule.event_date_by_index(lecture_num - 1) # Lecture numbers are base 1
			if i.type == "subsection"
				@root.add(Tree::TreeNode.new(i.subsection, i)) rescue binding.pry
			elsif i.type == "page"
				parent_tree_node = parent_node_of(i)
				parent_tree_node.add(Tree::TreeNode.new(i.identifier, i)) rescue binding.pry
				lecture_num += 1
			else
				raise ArgumentError, "invalid lecture page type of #{i.type}for #{i.title}"
			end
		end
	end

	def parent_node_of citem
		parent_node = @root[citem.subsection]
#		binding.pry if parent_node.nil?
		raise  RuntimeError, "Cant find section for item: #{citem.identifier}" if parent_node.nil?
		parent_node
	end

	def treenode_of citem
		@root.find do 
			|tree_node| 
				if citem.type == "subsection"
					citem.subsection == tree_node.name
				else 
					citem.identifier == tree_node.name
				end
		end
	end

	def subsections
		@root.children
	end

	def next_for(citem)
		next_node = treenode_of(citem).next_sibling rescue binding.pry
		if !next_node.nil?
			next_node.content
		else
			citem
		end
	end

	def previous_for(citem)
		prev_node = treenode_of(citem).previous_sibling
		if !prev_node.nil?
			prev_node.content
		else
			citem
		end
	end

	# def citem_lecture_number citem
	# 	citem.lecture_number
	# end

protected

	# def sort_items
	# 	@citems.sort_by { |i| [ i.type == "subsection" ? "A" : "B", i.order ] }
	# end

	def sort_items
		@citems.sort_by! { |i| [ lookup_citem_by_identifier(i.subsection).order, ((i.type == "page" ? 100 : 1 ) * i.order) ] }
	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coursegen-0.0.2 lib/coursegen/course/lib/lectures.rb