Class: Sinatra::Asterisk::SinatraManagerEventListener

Inherits:
Object
  • Object
show all
Includes:
ManagerEventListener
Defined in:
lib/sinatra/asterisk.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SinatraManagerEventListener

Returns a new instance of SinatraManagerEventListener.



56
57
58
# File 'lib/sinatra/asterisk.rb', line 56

def initialize(*args)
  @event_handlers, @sinatra_app = *args 
end

Instance Method Details

#eval_in_sinatra(event, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/sinatra/asterisk.rb', line 60

def eval_in_sinatra(event, &block)
    sinatra = @sinatra_app::new!
    sinatra_eigenclass = (class <<sinatra; self; end)
    sinatra_eigenclass.class_eval do
        # TODO: do this with a module ?
        define_method(:event) { event }
    end

    sinatra.instance_eval(&block)
end

#onManagerEvent(event) ⇒ Object

implements ManagerEventListener



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sinatra/asterisk.rb', line 72

def onManagerEvent(event)
  event_name = event.class.name.gsub(/^.*::/, '').gsub(/Event$/, '')
  catch(:halt) do
    block = @event_handlers.each do |event_class, block|
      catch :pass do
        throw :pass unless event_class.nil? || Utils::camelize(event_class) == event_name
        throw :halt, eval_in_sinatra(event, &block) 
      end
    end
  end
end