Skip to content

Commit

Permalink
Merge pull request #375 from romuloceccon/fix-true-class-warning
Browse files Browse the repository at this point in the history
Fix warning about usage of TrueClass#=~ on Ruby 2.7
  • Loading branch information
michaelklishin authored Apr 4, 2022
2 parents 7cd8596 + b765449 commit c3b9482
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/hutch/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ def self.is_bool(attr)
end

def self.to_bool(value)
!(value.nil? || value == '' || value =~ /^(false|f|no|n|0)$/i || value == false)
case value
when nil, false, '', /^(false|f|no|n|0)$/i
false
else
true
end
end

def self.is_num(attr)
Expand Down
48 changes: 43 additions & 5 deletions spec/hutch/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,52 @@
end

context 'boolean attributes' do
before { Hutch::Config.set(:autoload_rails, new_value) }
subject(:value) { Hutch::Config.user_config[:autoload_rails] }
context 'from non-empty string' do
before { Hutch::Config.set(:autoload_rails, new_value) }
subject(:value) { Hutch::Config.user_config[:autoload_rails] }

let(:new_value) { "t" }
let(:new_value) { "t" }


specify 'casts values to booleans' do
expect(value).to eq true
specify 'casts values to booleans' do
expect(value).to eq true
end
end

context 'from empty string' do
before { Hutch::Config.set(:autoload_rails, new_value) }
subject(:value) { Hutch::Config.user_config[:autoload_rails] }

let(:new_value) { "" }


specify 'casts values to booleans' do
expect(value).to eq false
end
end

context 'from boolean' do
before { Hutch::Config.set(:autoload_rails, new_value) }
subject(:value) { Hutch::Config.user_config[:autoload_rails] }

let(:new_value) { true }


specify 'casts values to booleans' do
expect(value).to eq true
end
end

context 'from nil' do
before { Hutch::Config.set(:autoload_rails, new_value) }
subject(:value) { Hutch::Config.user_config[:autoload_rails] }

let(:new_value) { nil }


specify 'casts values to booleans' do
expect(value).to eq false
end
end
end

Expand Down

0 comments on commit c3b9482

Please sign in to comment.