Skip to content

Instantly share code, notes, and snippets.

@jcfischer
Created May 1, 2012 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcfischer/2566051 to your computer and use it in GitHub Desktop.
Save jcfischer/2566051 to your computer and use it in GitHub Desktop.
First iteration of DSL to create Freeswitch apps
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
require_relative 'free_talk'
class MainMenuDsl
include FreeTalk
locale :de
scene "MainMenu" do
menu do
tries 3
text 'Press 1 for this, or 2 for that'
option 1 do
play 'pay_merchant'
end
option 2..9 do
play 'not_implemented'
end
option '*' do
play 'help'
end
end
end
scene 'help' do
speak 'This is the help text'
end
scene "pay_merchant" do
speak "Pay Merchant"
end
end
main = MainMenuDsl.play "MainMenu"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment