Skip to content

Commit

Permalink
feature: use native implementation for to_b instead active_record
Browse files Browse the repository at this point in the history
  • Loading branch information
artofhuman committed Apr 3, 2017
1 parent f3e4b89 commit 9e75fc0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build:
- /home/data/drone/key_cache:/ssh_keys
environment:
- COMPOSE_FILE_EXT=drone
- RUBY_IMAGE_TAG=2.2-latest
commands:
- wrapdocker docker -v

Expand Down
2 changes: 0 additions & 2 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
appraise 'rails3.2' do
gem 'activerecord', '~> 3.2.0'
gem 'actionpack', '~> 3.2.0'
gem 'activesupport', '~> 3.2.0'
end

appraise 'rails4.0' do
gem 'activerecord', '~> 4.0.0'
gem 'actionpack', '~> 4.0.0'
gem 'activesupport', '~> 4.0.0'
end
Expand Down
5 changes: 3 additions & 2 deletions lib/string_tools/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
require 'rchardet19'
require 'addressable/uri'
require 'active_support/core_ext/module'
require 'active_record'
require 'active_support/version'
require 'action_pack'
require 'string_tools/string'

class String
%w[auto_link excerpt highlight sanitize simple_format word_wrap].each do |method|
Expand All @@ -30,7 +31,7 @@ def to_f_with_strip_comma
alias_method_chain :to_f, :strip_comma

def to_b
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(self) || false
StringTools::String.new(self).to_b
end

def to_script_safe_json
Expand Down
20 changes: 20 additions & 0 deletions lib/string_tools/string.rb
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
15 changes: 15 additions & 0 deletions spec/string_tools/string_spec.rb
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
1 change: 0 additions & 1 deletion string_tools.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_runtime_dependency 'activerecord', '>= 3.1.12'
spec.add_runtime_dependency 'actionpack', '>= 3.1.12'
spec.add_runtime_dependency 'activesupport', '>= 3.1.12'
spec.add_runtime_dependency 'rchardet19', '~> 1.3.5'
Expand Down

0 comments on commit 9e75fc0

Please sign in to comment.