Sha256: f11d2fa48fca214b6a2eac89d76329b8771986903ed6b585ff72327905e85e9f

Contents?: true

Size: 1.87 KB

Versions: 18

Compression:

Stored size: 1.87 KB

Contents

require 'forwardable'
require 'byebug'

class Section
  extend Forwardable
  attr_reader :section
  def_delegators :@citems, :[], :count, :each, :sort!, :reduce

  def initialize(sect, citems, collapsed)
    @section = sect
    @collapsed = collapsed
    @citems = section_filter(citems)
    sort_pages
  end

  def find_index(citem)
    @citems.find_index(citem)
  end

  def find_by_short_name(sname)
    matches = @citems.select { |c| sname == c.short_name }
    raise RuntimeError,"'#{sname}': invalid reference in section \"#{@section}\"" if matches.length == 0
    raise RuntimeError, "'#{sname}': duplicate referenced in section \"#{@section}\"" if matches.length != 1
    matches[0]
  end

  def [](ind)
    @citems[ind]
  end

  def next_for(citem)
    index = @citems.find_index(citem)
    raise ArgumentError, "invalid citem in next_for" if index.nil?
    new_index = [index, @citems.length - 2].min
    @citems[new_index + 1]
  end

  def previous_for(citem)
    index = @citems.find_index(citem)
    byebug if index.nil?
    raise ArgumentError, "invalid citem in previous_for" if index.nil?
    new_index = [index, 1].max
    @citems[new_index - 1]
  end

  def has_subsections?
    false
  end

  def has_lecture_numbers?
    false
  end

  def collapsed?
    @collapsed
  end

  protected

  def lookup_citem_by_identifier identifier
    res = @citems.select { |i| i.identifier.to_s == identifier }
    fail "TOC#lookup_citem_by_identifier failed to find: '#{identifier}'"  if res.length != 1
    byebug if res.length != 1
    res[0]
  end

  # Remove citems that don't belong in this section, or are hidden
  def section_filter citems
    filtered_citems = citems.map do |citem|
      citem.section == @section && !citem.hidden? ? citem : nil
    end
    filtered_citems.compact
  end

  def sort_pages
    @citems.sort! { |a,b| a.order <=> b.order } rescue fail "sort_pages in section.rb"
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
coursegen-0.9.19 lib/coursegen/course/data/section.rb
coursegen-0.9.18 lib/coursegen/course/data/section.rb
coursegen-0.9.17 lib/coursegen/course/data/section.rb
coursegen-0.9.16 lib/coursegen/course/data/section.rb
coursegen-0.9.15 lib/coursegen/course/data/section.rb
coursegen-0.9.14 lib/coursegen/course/data/section.rb
coursegen-0.9.13 lib/coursegen/course/data/section.rb
coursegen-0.9.12 lib/coursegen/course/data/section.rb
coursegen-0.9.11 lib/coursegen/course/data/section.rb
coursegen-0.9.10 lib/coursegen/course/data/section.rb
coursegen-0.9.9 lib/coursegen/course/data/section.rb
coursegen-0.9.8 lib/coursegen/course/data/section.rb
coursegen-0.9.7 lib/coursegen/course/data/section.rb
coursegen-0.9.6 lib/coursegen/course/data/section.rb
coursegen-0.9.4 lib/coursegen/course/data/section.rb
coursegen-0.9.3 lib/coursegen/course/data/section.rb
coursegen-0.9.2 lib/coursegen/course/data/section.rb
coursegen-0.9.1 lib/coursegen/course/data/section.rb