Sha256: be07a01b1a2894fac2ef9bb28df5f7d6e2fadeccc6a8ee2373209b0bd01c7d6d
Contents?: true
Size: 1.57 KB
Versions: 1
Compression:
Stored size: 1.57 KB
Contents
class EventQueue def initialize @queue = Queue.new Thread.new do loop do check_for_events end end.priority = -1 end def event( event, messages ) @queue.enq [ event, messages ] end def watch_for( events , action_block ) events.each { |event| watchers_for(event) << action_block } end private def check_for_events event, messages = @queue.deq notify( event, *messages ) end def notify( event, *messages) watchers_for( event ).each { |action_block| action_block.call(event, *messages) } end def watchers_for( event ) watchers[ event ] ||= [] end def watchers @watchers ||= {} end end # This is a bit like observable, but for events module Notify def watch_for( *events , &action_block ) self.event_queue.watch_for( events, action_block ) end def notify( event, *messages) self.event_queue.event( event, messages ) end def event_queue @event_queue ||= EventQueue.new end end class String # Return the left bit of a string e.g. "String".left(2) => "St" def left( length ) self.slice( 0, length ) end # Encode the string so it can be used in urls (code coppied from CGI) def url_encode self.gsub(/([^a-zA-Z0-9_.-]+)/n) do '%' + $1.unpack('H2' * $1.size).join('%').upcase end.tr(' ', '+') end # Decode a string url encoded so it can be used in urls (code coppied from CGI) def url_decode self.gsub(/((?:%[0-9a-fA-F]{2})+)/n) do [$1.delete('%')].pack('H*') end end # Return the first n lines of the string def first_lines( lines = 1 ) self.split("\n")[0,lines].join("\n") end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
Soks-0.0.2 | lib/soks-utils.rb |