Sha256: a925dcb3ee22bd233d68c41c12f942c02b88c6c219b6eb10fd2a0a0da1f780d2

Contents?: true

Size: 878 Bytes

Versions: 3

Compression:

Stored size: 878 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|
      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

3 entries across 3 versions & 1 rubygems

Version Path
radiodan-0.0.3 lib/radiodan/event_binding.rb
radiodan-0.0.2 lib/radiodan/event_binding.rb
radiodan-0.0.1 lib/radiodan/event_binding.rb