Last active
December 16, 2015 01:38
-
-
Save stevenharman/5356166 to your computer and use it in GitHub Desktop.
A simple, purpose-built, logging abstraction used while importing BreweryDB.com data into Brewdega.
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
require 'delegate' | |
module SupplyChain | |
class Log < SimpleDelegator | |
def initialize(log = Rails.logger) | |
super(log) | |
end | |
def record(item) | |
debug(debug_message(item)) | |
error(error_message(item)) unless item.valid? | |
item | |
end | |
class Noop | |
def record(item) | |
item | |
end | |
end | |
private | |
def debug_message(item) | |
status = item.valid? ? 'SUCCEEDED' : 'FAILED' | |
"[SUPPLY_CHAIN] #{status}: #{short_description(item)}." | |
end | |
def error_message(item) | |
<<-MSG | |
[SUPPLY_CHAIN] FAILED: #{short_description(item)}. | |
#{item.errors.full_messages} | |
#{item.inspect} | |
MSG | |
end | |
def short_description(item) | |
"#{item.class} (#{item.id} :: #{item.brewery_db_id})" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment