From 3f13d962b60505fe91515f36ba65186c034212d7 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 18 Mar 2020 22:31:59 +0100 Subject: [PATCH] Refactor shell colors To not use a String to constantize anymore. --- lib/alchemy/shell.rb | 14 ++++++++------ spec/libraries/shell_spec.rb | 15 +++------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/alchemy/shell.rb b/lib/alchemy/shell.rb index c8bb8e270c..b15b2ebbd4 100644 --- a/lib/alchemy/shell.rb +++ b/lib/alchemy/shell.rb @@ -6,6 +6,13 @@ module Alchemy # in a list on the shell / log # module Shell + COLORS = { + clear: Thor::Shell::Color::CLEAR, + green: Thor::Shell::Color::GREEN, + red: Thor::Shell::Color::RED, + yellow: Thor::Shell::Color::YELLOW + }.freeze + def self.silence! @silenced = true end @@ -97,12 +104,7 @@ def log(message, type = nil) # @return [String] # def color(name) - color_const = name.to_s.upcase - if Thor::Shell::Color.const_defined?(color_const) - "Thor::Shell::Color::#{color_const}".constantize - else - "" - end + COLORS[name] end end end diff --git a/spec/libraries/shell_spec.rb b/spec/libraries/shell_spec.rb index 4b8d573c84..339d35e00f 100644 --- a/spec/libraries/shell_spec.rb +++ b/spec/libraries/shell_spec.rb @@ -99,23 +99,14 @@ class MyToDoList describe '.color' do context 'if given name is a constant of Thor::Shell::Color' do - before do - allow(Thor::Shell::Color).to receive(:const_defined?).and_return(true) - end - it "should call the constant" do - expect_any_instance_of(String).to receive(:constantize).and_return('') - MyToDoList.send(:color, :red) + expect(MyToDoList.send(:color, :red)).to eq(Thor::Shell::Color::RED) end end context 'if given name is not a defined constant of Thor::Shell::Color' do - before do - allow(Thor::Shell::Color).to receive(:const_defined?).and_return(false) - end - - it "should return en empty string" do - expect(MyToDoList.send(:color, :not_existing)).to eq('') + it "should return nil" do + expect(MyToDoList.send(:color, :not_existing)).to be_nil end end end