Sha256: f8df51449a5c3c142498dbfb9cf92c52dee89333de381ec751162c6e986091a7
Contents?: true
Size: 1.37 KB
Versions: 1
Compression:
Stored size: 1.37 KB
Contents
require "wisper/version" module Wisper include ActiveSupport::Concern def listeners @listeners ||= Set.new end def add_listener(listener, options = {}) listeners << ObjectRegistration.new(listener, options) end def add_block_listener(options = {}, &block) listeners << BlockRegistration.new(block, options) end # sugar def respond_to(event, &block) listeners << BlockRegistration.new(block, :on => event) end class BlockRegistration attr_reader :on, :listener def initialize(block, options) @listener = block @on = Array(options.fetch(:on) { 'all' }).map(&:to_s) end def broadcast(event, *args) if on.include?(event) || on.include?('all') listener.call(*args) end end end class ObjectRegistration attr_reader :on, :listener def initialize(listener, options) @listener = listener @method = options[:method] @on = Array(options.fetch(:on) { 'all' }).map(&:to_s) end def broadcast(event, *args) if (on.include?(event) || on.include?('all')) && listener.respond_to?(event) listener.public_send(event, *args) end end end private def broadcast(event, *args) listeners.each do | listener | listener.broadcast(clean_event(event), *args) end end def clean_event(event) event.to_s.gsub('-', '_') end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
wisper-0.0.1 | lib/wisper.rb |