Skip to content

Commit

Permalink
refactor: use lab_name instead of lab, preparing for proper Lab object
Browse files Browse the repository at this point in the history
  • Loading branch information
alexch committed May 24, 2012
1 parent 7cb896a commit c71ba24
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app.rb
Expand Up @@ -63,14 +63,14 @@
get "/live/:course/:lab" do
Live.new(
:course => Course.new(params[:course]),
:lab => params[:lab]
:lab_name => params[:lab]
).to_pretty
end

get "/live" do
Live.new(
:course => Course.new("learn_ruby"),
:lab => "hello"
:lab_name => "hello"
).to_pretty
end

35 changes: 18 additions & 17 deletions lib/course.rb
Expand Up @@ -8,9 +8,10 @@ class Course
include Erector::Mixin

# todo: move to Curriculum object
def self.all_labs(curriculum_name)
# todo: rename Curriculum to Area
def self.all_lab_names(curriculum_name)
curriculum_dir = "#{Course.root}/#{curriculum_name}"
labs = Dir.glob("#{curriculum_dir}/*").
lab_names = Dir.glob("#{curriculum_dir}/*").
select {|d| File.directory?(d)}.
map {|d| d.split('/').last}.
reject {|d| d == "ubiquitous" || d == "assets"}.
Expand All @@ -22,7 +23,7 @@ def self.root
File.expand_path("#{here}/..")
end

attr_accessor :curriculum_name, :course_name, :labs, :repo, :repo_dir, :repo_parent
attr_accessor :curriculum_name, :course_name, :lab_names, :repo, :repo_dir, :repo_parent

def initialize(file)
if file.is_a? String
Expand All @@ -32,7 +33,7 @@ def initialize(file)
data = YAML::load(file.read)
@curriculum_name = data[:curriculum] || "learn_ruby"
@course_name = file.path.split('/').last.gsub(/\.yaml/, '')
@labs = data[:labs]
@lab_names = data[:labs]
@repo = data[:repo] || "git@github.com:alexch/#{@course_name}.git"
@repo_parent =
File.expand_path "#{Course.root}/.."
Expand Down Expand Up @@ -75,10 +76,10 @@ def build
copy_files curriculum_dir, repo_dir, 0
copy_files assets_dir, "#{repo_dir}/assets"

@labs.each_with_index do |lab, i|
@lab_names.each_with_index do |lab_name, i|
num = "%02d" % i
numbered = "#{num}_#{lab}"
source_dir = lab_dir(lab)
numbered = "#{num}_#{lab_name}"
source_dir = lab_dir(lab_name)
target_dir = "#{@repo_dir}/#{numbered}"
raise "Missing lab #{source_dir}" unless File.exist? source_dir and File.directory? source_dir
FileUtils.touch "#{source_dir}/index.md" unless File.exist?("#{source_dir}/index.md")
Expand Down Expand Up @@ -128,20 +129,20 @@ def copy_files(source_dir, target_dir, level = 1)
markdown_files.each do |markdown_file|
html_file = target_dir + "/" + markdown_file.gsub(/\.md$/, '.html')
prefix = "../" * level
current_lab = source_dir.split('/').last
current_lab_name = source_dir.split('/').last

markdown = File.read("#{source_dir}/#{markdown_file}")
html = Markdown.new(markdown).to_html

File.open(html_file, "w") do |f|
f.print Course::Page.new(
:course_name => course_name,
:current_lab => current_lab,
:current_lab_name => current_lab_name,
:prefix => prefix,
:level => level,
:source_dir => source_dir,
:html => html,
:labs => @labs
:lab_names => @lab_names
).to_pretty

end
Expand All @@ -150,13 +151,13 @@ def copy_files(source_dir, target_dir, level = 1)

class Page < Erector::Widget
# todo: fewer parameters, ideally just a Course and a current_lab
needs :course_name, :current_lab, :prefix, :level, :source_dir, :html, :labs
needs :course_name, :current_lab_name, :prefix, :level, :source_dir, :html, :lab_names

def content
html do
head do
title do
text "Test-First Teaching: ", @course_name, ": ", @current_lab
text "Test-First Teaching: ", @course_name, ": ", @current_lab_name
end
link :href => "#{@prefix}assets/style.css", :media => 'screen', :rel => 'stylesheet', :type => 'text/css'
end
Expand Down Expand Up @@ -189,12 +190,12 @@ def nav_bar
h2 { a @course_name, :href=> ("../" * @level) + 'index.html' }
b "Labs:"
ul {
@labs.each_with_index do |lab, i|
@lab_names.each_with_index do |lab_name, i|
num = "%02d" % i
numbered = "#{num}_#{lab}"
titled = "#{num} #{lab.split('_').map{|s|s.capitalize}.join(' ')}"
numbered = "#{num}_#{lab_name}"
titled = "#{num} #{lab_name.split('_').map{|s|s.capitalize}.join(' ')}"
li {
if @current_lab == lab
if @current_lab_name == lab_name
text titled
else
href = (if @level == 0
Expand All @@ -212,7 +213,7 @@ def nav_bar

def lab_info
div(:class => "info") {
h1 @current_lab
h1 @current_lab_name
ul {
files = Dir.glob("#{@source_dir}/*")
files.each do |file|
Expand Down
10 changes: 5 additions & 5 deletions spec/course_spec.rb
Expand Up @@ -10,24 +10,24 @@
assert { Course.root.split('/').last == "test-first-teaching" }
end

it "lists all labs under the given curriculum" do
labs = Course.all_labs("sample_curriculum")
assert { labs == ["one", "three", "two"] }
it "lists all lab names under the given curriculum" do
lab_names = Course.all_lab_names("sample_curriculum")
assert { lab_names == ["one", "three", "two"] }
end

it 'initializes from a course yaml file' do
c = Course.new(File.new("#{Course.root}/courses/sample_course.yaml"))
assert { c.curriculum_name == "sample_curriculum" }
assert { c.course_name == "sample_course" }
assert { c.labs == ["one", "two", "three"] }
assert { c.lab_names == ["one", "two", "three"] }
assert { c.repo == "git@github.com:alexch/sample_course.git" }
end

it 'initializes from a course name (and looks in the courses dir for its yaml file)' do
c = Course.new("sample_course")
assert { c.curriculum_name == "sample_curriculum" }
assert { c.course_name == "sample_course" }
assert { c.labs == ["one", "two", "three"] }
assert { c.lab_names == ["one", "two", "three"] }
assert { c.repo == "git@github.com:alexch/sample_course.git" }
end

Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
@@ -1,14 +1,12 @@
require 'spork'


Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
require 'rspec/core'
require 'wrong'
include Wrong
Wrong.config.verbose
Wrong.config.color
end

Expand Down
18 changes: 9 additions & 9 deletions web/live.rb
Expand Up @@ -157,19 +157,19 @@ class Live < Erector::Widgets::Page
}
CSS

needs :course, :lab => "hello"
needs :course, :lab_name => "hello"

def initialize *args
super
@notes = ("#{lab_dir}/index.md" if File.exist? "#{lab_dir}/index.md")
end

def lab_dir lab_name = @lab
def lab_dir lab_name = @lab_name
@course.lab_dir(lab_name)
end

def test
"#{lab_dir}/#{lab}_spec.rb"
"#{lab_dir}/#{lab_name}_spec.rb"
end

def page_title
Expand Down Expand Up @@ -203,14 +203,14 @@ def labs_panel
div.toggleable.labs! do
h2 "Labs"
ul do
current_lab = @lab
@course.labs.each do |lab|
current_lab = @lab_name
@course.lab_names.each do |lab_name|
li do
# todo: pretty lab name; Lab object
if lab == @lab
span.current lab
if lab_name == @lab_name
span.current lab_name
else
a lab, :href => "/live/#{@course.course_name}/#{lab}"
a lab_name, :href => "/live/#{@course.course_name}/#{lab_name}"
end
end
end
Expand Down Expand Up @@ -245,7 +245,7 @@ def body_content

div.panels {
div.user {
panel "Tests", :code => File.read(@course.lab_dir(@lab) + "/#{@lab}_spec.rb").
panel "Tests", :code => File.read(@course.lab_dir(@lab_name) + "/#{@lab_name}_spec.rb").
gsub(/require [\"\'].*[\"\'].*\n/, '') # todo: allow some requires
panel "Source", :code => ""
panel "Results", :wide => true
Expand Down

0 comments on commit c71ba24

Please sign in to comment.