class MTK::Lang::TutorialLesson

@private

Public Class Methods

new(options) click to toggle source

Every TutorialLesson requires the options at construction title:

title - The short summary displayed in the tutorial's table of contents.
description - The full description displayed when entering the tutorial step.
validate(input) - Validate user input entered after the description is displayed.
success(input) - Perform an action on successful validation
failure(input) - Instruct the user on failed validation
# File lib/mtk/lang/tutorial_lesson.rb, line 13
def initialize options
  @title = options[:title]
  @description = options[:description].split("\n").map{|line| line.strip }.join("\n") # trim extra whitespace
  @validation = options[:validation]
end

Public Instance Methods

failure(input) click to toggle source
# File lib/mtk/lang/tutorial_lesson.rb, line 74
def failure(input)
  puts
  puts "Invalid entry \"#{input}\"".bold.red
end
run(output) click to toggle source
# File lib/mtk/lang/tutorial_lesson.rb, line 20
def run(output)
  puts
  puts Tutorial::SEPARATOR
  puts
  puts "Lesson: #{@title}".bold.yellow
  puts @description
  puts
  print "Try it now: ".blue

  did_it_once = false
  input = gets.strip
  until did_it_once and input.empty?
    until validate(input)
      failure(input)
      print "Try again: ".blue
      input = gets.strip
    end

    success(input, output)
    did_it_once = true
    puts
    puts "Good! ".bold.green + "Try again, or press enter to exit this lesson:".blue
    input = gets.strip
  end
end
success(input, output) click to toggle source
# File lib/mtk/lang/tutorial_lesson.rb, line 62
def success(input, output)
  sequencer = MTK::Lang::Parser.parse(input)
  if sequencer
    output.play sequencer.to_timeline
  else
    STDERR.puts "Nothing to play for \"#{input}\""
  end
rescue Citrus::ParseError
  STDERR.puts $!
end
to_s() click to toggle source
# File lib/mtk/lang/tutorial_lesson.rb, line 80
def to_s
  @title
end
validate(input) click to toggle source
# File lib/mtk/lang/tutorial_lesson.rb, line 47
def validate(input)
  return false if input.empty?

  case @validation
    when Symbol
      MTK::Lang::Parser.parse(input, @validation)
      true
    else # Assume Regexp
      (input =~ @validation) != nil and MTK::Lang::Parser.parse(input)
  end
rescue Citrus::ParseError
  false
end