Created
June 7, 2017 08:48
-
-
Save jikkujose/308f7ff791def5a1dc24d3d6ee6f80fa to your computer and use it in GitHub Desktop.
Weird switch
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 'colorize' | |
COLORS = { | |
log: :blue, | |
error: :red, | |
ok: :green | |
} | |
module Validator | |
def valid_type?(type) | |
unless COLORS.include?(type) | |
fail "Unknown type: #{type}" | |
end | |
end | |
end | |
class Logger | |
extend Validator | |
def self.log(**params) | |
valid_type?(params[:type]) | |
new | |
.output(**params) | |
end | |
def output(prefix: 'PREFIX', message:, type: :ok) | |
[ | |
console(type), | |
console_prefix(prefix: prefix, type: type), | |
console_message(message: message, type: type), | |
].compact.join(" ") | |
end | |
def console(type) | |
type | |
.to_s | |
.upcase | |
.send("on_#{COLORS[type]}") | |
.bold | |
end | |
def console_prefix(prefix: nil, type: :log) | |
"#{prefix}:" | |
.send(COLORS[type]) | |
.bold | |
end | |
def console_message(message: nil, type: :error) | |
message | |
.to_s | |
.red if type == :error | |
end | |
end | |
puts Logger.log( | |
prefix: 'Jikku', | |
message: 'Thats cool!', | |
type: :log | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment