Skip to content

Commit

Permalink
Refactor shell colors
Browse files Browse the repository at this point in the history
To not use a String to constantize anymore.
  • Loading branch information
tvdeyen committed Mar 18, 2020
1 parent 6539f2f commit 3f13d96
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
14 changes: 8 additions & 6 deletions lib/alchemy/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
15 changes: 3 additions & 12 deletions spec/libraries/shell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f13d96

Please sign in to comment.