-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: use native implementation for to_b instead active_record
- Loading branch information
1 parent
f3e4b89
commit 9e75fc0
Showing
6 changed files
with
39 additions
and
5 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,20 @@ | ||
module StringTools | ||
class String | ||
TRUE_VALUES = %w(1 t T true TRUE on ON).to_set | ||
|
||
def initialize(string) | ||
@string = string | ||
end | ||
|
||
# Public: cast string value to boolean | ||
# | ||
# Example: | ||
# StringTools::String.new('t').to_b | ||
# #=> true | ||
# | ||
# Return boolean | ||
def to_b | ||
TRUE_VALUES.include?(@string) | ||
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,15 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe StringTools::String, '#to_b' do | ||
["0", "f", "F", "false", "FALSE", "off", "OFF", "", "2017-01-01"].each do |val| | ||
it "returns false" do | ||
expect(described_class.new(val).to_b).to be false | ||
end | ||
end | ||
|
||
%w(1 t T true TRUE on ON).each do |val| | ||
it "returns true" do | ||
expect(described_class.new(val).to_b).to be true | ||
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