Created
January 27, 2018 08:26
-
-
Save KelseyDH/c7aabc918644e0536a7ee865c39132b9 to your computer and use it in GitHub Desktop.
How to write a Rails Service Object with logging
This file contains 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
# # A Plain Old Ruby Object (poro) with Rails logger support: | |
class ServiceObject | |
def initialize(foo) | |
@logger = Logger.new(STDOUT) | |
@foo = foo | |
end | |
def call | |
do_something | |
end | |
private | |
def do_something | |
@logger.debug { "do_something called on #{@foo.class}" } | |
end | |
end | |
class Bar | |
end | |
# # Usage example: | |
MyService.new(Bar.new).call | |
#=> [2018-01-27] DEBUG -- : do_something called! With Bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment