diff --git a/.drone.yml b/.drone.yml index 4a253a9..279c99e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 diff --git a/Appraisals b/Appraisals index e17b0cb..624e33d 100644 --- a/Appraisals +++ b/Appraisals @@ -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 diff --git a/lib/string_tools/core_ext/string.rb b/lib/string_tools/core_ext/string.rb index 9b1c956..bbef1a9 100644 --- a/lib/string_tools/core_ext/string.rb +++ b/lib/string_tools/core_ext/string.rb @@ -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| @@ -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 diff --git a/lib/string_tools/string.rb b/lib/string_tools/string.rb new file mode 100644 index 0000000..25a8c4c --- /dev/null +++ b/lib/string_tools/string.rb @@ -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 diff --git a/spec/string_tools/string_spec.rb b/spec/string_tools/string_spec.rb new file mode 100644 index 0000000..766be8b --- /dev/null +++ b/spec/string_tools/string_spec.rb @@ -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 diff --git a/string_tools.gemspec b/string_tools.gemspec index 28bd364..dea96f8 100644 --- a/string_tools.gemspec +++ b/string_tools.gemspec @@ -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'