Sha256: 31f00109d22d5cd8c5effbcf4c2f5f26df4be1ff8b800b2c647dbbad9e4e9ba6

Contents?: true

Size: 895 Bytes

Versions: 1

Compression:

Stored size: 895 Bytes

Contents

class Radiodan
module EventBinding
  def register_event(event, &blk)
    logger.info "Registered event #{event}"
    event = event.to_sym
    event_bindings[event] << blk
    
    true
  end
  
  def trigger_event(event, data=nil)
    event = event.to_sym
    bindings = event_bindings[event]
    
    unless bindings
      logger.error "Event #{event} triggered but not found" 
    end
    
    bindings.each do |blk|
      EM.next_tick { blk.call(data) }
    end
  end
  
  def events
    event_bindings.keys.sort
  end
  
  def respond_to?(method)
    if event_bindings.include?(method)
      true
    else
      super
    end
  end
  
  private
  def event_bindings
    @event_bindings ||= Hash.new{ |h, k| h[k] = [] }
  end
  
  def method_missing(method, *args, &block)
    if event_bindings.include?(method)
      trigger_event(method, *args)
    else
      super
    end
  end  
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radiodan-0.0.4 lib/radiodan/event_binding.rb