Last active
June 10, 2016 09:03
-
-
Save wojtha/da89c7895d600a3b16f3bbe28bf575f2 to your computer and use it in GitHub Desktop.
Fail fast with wisper events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'wisper' | |
| module FailFastWisper | |
| def self.included(base) | |
| base.include Wisper::Publisher | |
| base.include Helpers | |
| base.prepend Proxy | |
| end | |
| module Proxy | |
| def call(*) | |
| catch(:halt) do | |
| super | |
| end | |
| end | |
| end | |
| private_constant :Proxy | |
| module Helpers | |
| def rescue_halt | |
| catch(:halt) do | |
| yield | |
| end | |
| end | |
| def halt! | |
| throw :halt | |
| end | |
| def emit(*args) | |
| broadcast(*args) | |
| end | |
| def emit!(*args) | |
| broadcast(*args) | |
| halt! | |
| end | |
| def failure!(*args) | |
| failure(*args) | |
| halt! | |
| end | |
| def failure(*args) | |
| broadcast(:failure, *args) | |
| end | |
| def warning(*args) | |
| broadcast(:warning, *args) | |
| end | |
| def success(*args) | |
| broadcast(:success, *args) | |
| end | |
| end | |
| private_constant :Helpers | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SomeOperation | |
| include FailFastWisper | |
| def call(error_level = 0) | |
| failure! "Fatal error!" if error_level > 1 | |
| if error_level > 0 | |
| failure "Warning!" | |
| emit :custom_warning, "Achtung!" | |
| else | |
| success "hello!" | |
| end | |
| end | |
| end | |
| service = SomeOperation.new | |
| service.on(:success) do |*args| | |
| puts "SUCCESS! #{args.inspect}" | |
| end | |
| service.on(:custom_warning) do |*args| | |
| puts "CUSTOM! #{args.inspect}" | |
| end | |
| service.on(:failure) do |*args| | |
| puts "FAILURE! #{args.inspect}" | |
| end | |
| puts "(0) =================================" | |
| service.call(0) | |
| # SUCCESS! ["hello!"] | |
| puts "(1) =================================" | |
| service.call(1) | |
| # FAILURE! ["Warning!"] | |
| # CUSTOM! ["Achtung!"] | |
| puts "(2) =================================" | |
| service.call(2) | |
| # FAILURE! ["Fatal error!"] | |
| service = SomeOperation.new | |
| .on(:success) { |*args| puts "SUCCESS! #{args.inspect}" } | |
| .on(:custom_warning) { |*args| puts "CUSTOM! #{args.inspect}" } | |
| .on(:failure) { |*args| puts "FAILURE! #{args.inspect}" } | |
| puts "(3) =================================" | |
| service.call(0) | |
| # SUCCESS! ["hello!"] | |
| puts "(4) =================================" | |
| service.call(1) | |
| # FAILURE! ["Warning!"] | |
| # CUSTOM! ["Achtung!"] | |
| puts "(5) =================================" | |
| service.call(2) | |
| # FAILURE! ["Fatal error!"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment