Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #10402: add to_bool function for converting to boolean.
This commit adds a to_bool function for casting values to boolean as well as an example usage. http://projects.theforeman.org/issues/10402
- Loading branch information
Walden Raines
authored and
Dominic Cleal
committed
May 19, 2015
1 parent
ee6fc20
commit c74610f
Showing
6 changed files
with
75 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Foreman | ||
module Cast | ||
def self.to_bool(value) | ||
case value | ||
|
||
when String | ||
return true if value =~ (/\A(true|t|yes|y|on|1)\z/i) | ||
return false if value.blank? || value =~ (/\A(false|f|no|n|off|0)\z/i) | ||
return nil | ||
|
||
when Fixnum | ||
return true if value == 1 | ||
return false if value == 0 | ||
|
||
when NilClass | ||
return false | ||
|
||
when TrueClass, FalseClass | ||
return value | ||
|
||
else | ||
return nil | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'test_helper' | ||
require 'foreman/cast' | ||
|
||
class CastTest < ActiveSupport::TestCase | ||
include Foreman::Cast | ||
|
||
test "should convert strings to booleans" do | ||
true_strings = %w(true t yes y on 1) | ||
false_strings = %w(false f no n off 0) | ||
|
||
true_strings.each do |true_string| | ||
assert_equal true, Foreman::Cast.to_bool(true_string) | ||
end | ||
|
||
false_strings.each do |false_string| | ||
assert_equal false, Foreman::Cast.to_bool(false_string) | ||
end | ||
end | ||
|
||
test "should convert FixNums to booleans" do | ||
assert_equal true, Foreman::Cast.to_bool(1) | ||
assert_equal false, Foreman::Cast.to_bool(0) | ||
end | ||
|
||
test "should convert Nil to boolean" do | ||
assert_equal false, Foreman::Cast.to_bool(nil) | ||
end | ||
|
||
test "should return TrueClass if TrueClass" do | ||
assert_equal true, Foreman::Cast.to_bool(true) | ||
end | ||
|
||
test "should return FalseClass if FalseClass" do | ||
assert_equal false, Foreman::Cast.to_bool(false) | ||
end | ||
end | ||
|
This file contains 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