Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Last active September 16, 2015 15:42
Show Gist options
  • Save joshnesbitt/2706be8b5e3615b60b1c to your computer and use it in GitHub Desktop.
Save joshnesbitt/2706be8b5e3615b60b1c to your computer and use it in GitHub Desktop.

Revisions

  1. joshnesbitt revised this gist Sep 16, 2015. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions run.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    require_relative('pub_sub.rb')

    Hub.subscribe('user:register') do |payload|
    puts "User with and ID of #{payload[:id]} registered."
    end

    Hub.subscribe('post:created') do |payload|
    puts "Post with and ID of #{payload[:id]} created."
    end

    Hub.subscribe('test') do |payload|
    puts "Hi there #{payload[:name]}!"
    end

    Hub.publish('user:register', id: 1)
    Hub.publish('post:created', id: 2)
    Hub.publish('test', name: 'Bob')

    puts "Current namespaces:"
    puts Hub.namespaces
  2. joshnesbitt revised this gist Sep 16, 2015. 1 changed file with 0 additions and 19 deletions.
    19 changes: 0 additions & 19 deletions pub_sub.rb
    Original file line number Diff line number Diff line change
    @@ -27,22 +27,3 @@ def handlers
    end
    end
    end

    Hub.subscribe('user:register') do |payload|
    puts "User with and ID of #{payload[:id]} registered."
    end

    Hub.subscribe('post:created') do |payload|
    puts "Post with and ID of #{payload[:id]} created."
    end

    Hub.subscribe('test') do |payload|
    puts "Hi there #{payload[:name]}!"
    end

    Hub.publish('user:register', id: 1)
    Hub.publish('post:created', id: 2)
    Hub.publish('test', name: 'Bob')

    puts "Current namespaces:"
    puts Hub.namespaces
  3. joshnesbitt revised this gist Sep 16, 2015. No changes.
  4. joshnesbitt created this gist Sep 16, 2015.
    48 changes: 48 additions & 0 deletions pub_sub.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    class Hub
    class << self
    NAMESPACE_SEPARATOR = ':'

    def publish(event, payload = {})
    handlers[event].each do |handler|
    handler.call(payload)
    end
    end

    def subscribe(event, &block)
    handlers[event] << block
    end

    def namespaces
    names = handlers.keys
    names.map! { |k| k.split(NAMESPACE_SEPARATOR).first }
    names.uniq
    end

    private

    def handlers
    @handlers ||= Hash.new do |hash, key|
    hash[key] = []
    end
    end
    end
    end

    Hub.subscribe('user:register') do |payload|
    puts "User with and ID of #{payload[:id]} registered."
    end

    Hub.subscribe('post:created') do |payload|
    puts "Post with and ID of #{payload[:id]} created."
    end

    Hub.subscribe('test') do |payload|
    puts "Hi there #{payload[:name]}!"
    end

    Hub.publish('user:register', id: 1)
    Hub.publish('post:created', id: 2)
    Hub.publish('test', name: 'Bob')

    puts "Current namespaces:"
    puts Hub.namespaces