Skip to content

Instantly share code, notes, and snippets.

@benlangfeld
Created March 29, 2011 10:00
Show Gist options
  • Save benlangfeld/892114 to your computer and use it in GitHub Desktop.
Save benlangfeld/892114 to your computer and use it in GitHub Desktop.
class Loader
class << self
attr_accessor :default_dial_plan_file_name
def load(dial_plan_as_string)
string_io = StringIO.new dial_plan_as_string
def string_io.path
"(eval)"
end
load_dialplans string_io
end
def load_dialplans(*files)
files = Adhearsion::AHN_CONFIG.files_from_setting("paths", "dialplan") if files.empty?
files = Array files
files.map! do |file|
case file
when File, StringIO
file
when String
File.new file
else
raise ArgumentError, "Unrecognized type of file #{file.inspect}"
end
end
new.tap do |loader|
files.each do |file|
loader.load file
end
end
end
end
self.default_dial_plan_file_name ||= 'dialplan.rb'
def initialize
@context_collector = ContextNameCollector.new
end
def contexts
@context_collector.contexts
end
def load(dialplan_file)
dialplan_code = dialplan_file.read
@context_collector.instance_eval(dialplan_code, dialplan_file.path)
nil
end
class ContextNameCollector# < ::BlankSlate
class << self
def const_missing(name)
super
rescue ArgumentError
raise NameError, %(undefined constant "#{name}")
end
end
attr_reader :contexts
def initialize
@contexts = {}
end
def method_missing(name, *args, &block)
super if !block_given? || args.any?
contexts[name] = DialplanContextProc.new(name, &block)
end
end
end
class DialplanContextProc < Proc
attr_reader :name
def initialize(name, &block)
super(&block)
@name = name
end
def +@
raise Adhearsion::VoIP::DSL::Dialplan::ControlPassingException.new(self)
end
end
Failure/Error: manager = Adhearsion::DialPlan::Manager.new
ArgumentError:
method 'initialize': given 1, expected 0
# kernel/common/proc.rb:40:in `new'
# ./lib/adhearsion/voip/dial_plan.rb:230:in `am_not_for_kokoa! (method_missing)'
# dialplan.rb:1:in `load'
# kernel/common/block_environment.rb:52:in `call_on_instance'
# kernel/common/eval.rb:194:in `instance_eval'
# ./lib/adhearsion/voip/dial_plan.rb:207:in `load'
# ./lib/adhearsion/voip/dial_plan.rb:188:in `load_dialplans'
# kernel/bootstrap/array.rb:71:in `each'
# ./lib/adhearsion/voip/dial_plan.rb:187:in `load_dialplans'
# kernel/common/kernel.rb:358:in `tap'
# ./lib/adhearsion/voip/dial_plan.rb:186:in `load_dialplans'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment