Skip to content

Commit

Permalink
Use blacklist for bool type cast
Browse files Browse the repository at this point in the history
  • Loading branch information
jgraichen committed May 31, 2016
1 parent de02087 commit c720a9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/acfs/resource/attributes/boolean.rb
Expand Up @@ -14,7 +14,7 @@ module Acfs::Resource::Attributes
# true, on, yes
#
class Boolean < Base
TRUE_VALUES = [true, 1, '1', 'on', 'yes', 'true']
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO'].to_set

# @api public
#
Expand All @@ -30,7 +30,7 @@ def cast_value(value)
if value.blank?
nil
else
TRUE_VALUES.include? value
!FALSE_VALUES.include?(value)
end
end
end
Expand Down
33 changes: 25 additions & 8 deletions spec/acfs/resource/attributes/boolean_spec.rb
Expand Up @@ -21,19 +21,36 @@
expect(subject.cast(true)).to eq true
end

it 'casts TRUE_VALUES to true' do
it 'casts falsy values to false' do
expect(subject.cast(false)).to eq false
expect(subject.cast(0)).to eq false
expect(subject.cast('0')).to eq false
expect(subject.cast('no')).to eq false
expect(subject.cast('NO')).to eq false
expect(subject.cast('off')).to eq false
expect(subject.cast('OFF')).to eq false
expect(subject.cast('false')).to eq false
expect(subject.cast('FALSE')).to eq false
expect(subject.cast('f')).to eq false
expect(subject.cast('F')).to eq false
end

it 'casts any other value to true' do
expect(subject.cast(true)).to eq true
expect(subject.cast(1)).to eq true
expect(subject.cast('1')).to eq true
expect(subject.cast('yes')).to eq true
expect(subject.cast('YES')).to eq true
expect(subject.cast('on')).to eq true
expect(subject.cast('ON')).to eq true
expect(subject.cast('true')).to eq true
expect(subject.cast('1')).to eq true
end
expect(subject.cast('TRUE')).to eq true
expect(subject.cast('t')).to eq true
expect(subject.cast('T')).to eq true

it 'casts any other value to false' do
expect(subject.cast(0)).to eq false
expect(subject.cast(2)).to eq false
expect(subject.cast('wrong')).to eq false
expect(subject.cast('random')).to eq false
expect(subject.cast(2)).to eq true
expect(subject.cast('wrong')).to eq true
expect(subject.cast('random')).to eq true
end
end
end

0 comments on commit c720a9c

Please sign in to comment.