Created
May 1, 2012 07:54
-
-
Save jcfischer/2566051 to your computer and use it in GitHub Desktop.
First iteration of DSL to create Freeswitch apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoSceneDefinedError < RuntimeError; | |
end | |
class Scene | |
attr_accessor :function | |
def initialize name | |
@name = name | |
@function = nil | |
end | |
def menu options = {}, &block | |
menu = Menu.new | |
@function = menu | |
menu.instance_eval(&block) | |
puts 'in menu definition' | |
menu | |
end | |
def speak text | |
puts "saying: #{text}" | |
end | |
def execute | |
puts "- Executing Scene #@name" | |
@function.run | |
end | |
end | |
class Menu | |
attr_accessor :menu, :tries, :text, :options | |
def initialize | |
@menu = nil | |
@tries = 1 | |
@text = "Default Menu Text" | |
@options = {} | |
end | |
def add_option digit, &block | |
@options[digit] = block | |
end | |
def tries number | |
puts "setting tries: #{number}" | |
@tries = number | |
self | |
end | |
def option digit, &block | |
puts "defining option #{}" | |
add_option digit, &block | |
self | |
end | |
def text text | |
puts "defining text #{text}" | |
@text = text | |
self | |
end | |
def run | |
tries = 0 | |
while tries < @tries do | |
puts "* #@text" | |
choice = gets.chomp.to_i | |
puts "user choose: #{choice}" | |
options[choice].call | |
end | |
end | |
end | |
module FreeTalk | |
def self.included traitable_class | |
puts 'including freeTalk' | |
traitable_class.extend ClassMethods | |
end | |
module ClassMethods | |
def scene name, &block | |
puts "scene #{name}" | |
@scenes ||= {} | |
scene = Scene.new(name) | |
@current_scene = @scenes[name] = scene | |
scene.instance_eval(&block) | |
scene | |
end | |
def scenes | |
@scenes | |
end | |
def locale locale | |
@locale = locale | |
end | |
def play scene | |
puts "playing #{scene}" | |
# puts @scenes.inspect | |
if scenes[scene] | |
scenes[scene].execute | |
else | |
raise NoSceneDefinedError.new scene | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment