Skip to content

Instantly share code, notes, and snippets.

@kigster
Last active February 8, 2019 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kigster/9c5fcef52407acb49d1411d06b73f57e to your computer and use it in GitHub Desktop.
Save kigster/9c5fcef52407acb49d1411d06b73f57e to your computer and use it in GitHub Desktop.
Exploration of the Ruby Gem Wisper in the context of class-level subscriptions
#!/usr/bin/env ruby
#
# License: MIT
# Author: Konstantin Gredeskoul
#
# This is a simple conceptual script to explore the gem Wisper, but unlike the examples in the README
# using the class methods and class-level subscriptions similar to how another gem Ventable
# accomplishes something similar. Wisper passes with flying colors.
#
# ## Usage
#
# ```bash
# gem install wisper colored2
# # download the gist to wispertest.rb
# ruby wispertest.rb 100
# ruby wispertest.rb 101
# ```
#
require 'wisper'
require 'colored2'
class EmailService
def self.form_published(id)
puts "EmailService: sending email because form #{id} is published".green
end
end
class ErrorLogger
def self.form_publishing_failed(id, msg = '')
puts "ErrorLogger: logging form publishing failure for form #{id} #{msg.yellow}".red
end
end
class FormsService
extend Wisper::Publisher
subscribe EmailService
subscribe ErrorLogger
def self.activate(id)
id.even? ? broadcast(:form_published, id) :
broadcast(:form_publishing_failed, id, 'form publishing failed because its ID was odd...')
end
end
class Main
def self.call(id)
FormsService.on(:form_published) { |id| puts "main: form #{id} publishing succeeded!" }
FormsService.on(:form_publishing_failed) { |id| puts "main: form #{id} pubshing broke" }
FormsService.activate(id)
FormsService.activate(id + 10001)
end
end
ARGV.empty? ? puts("usage: #{$0} id"): Main.call(ARGV.first.to_i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment