Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow case-insensitive Dictionary#lookup

  • Loading branch information...
commit 81da1881fb8e2382ed426383c362d81ef45ddc9c 1 parent d1f29d5
@ihough ihough authored
View
13 lib/data_miner/dictionary.rb
@@ -19,6 +19,10 @@ def sprintf
options['sprintf'] || '%s'
end
+ def case_sensitive
+ true unless options['case_sensitive'] == false
+ end
+
def table
@table ||= ::RemoteTable.new(options['url']).to_a # convert to Array immediately
end
@@ -27,9 +31,9 @@ def free
@table.free if @table.is_a?(::RemoteTable)
@table = nil
end
-
+
def lookup(key)
- find key_name, key, value_name, 'sprintf' => sprintf
+ find key_name, key, value_name, {'sprintf' => sprintf, 'case_sensitive' => case_sensitive}
end
def find(key_name, key, value_name, options = {})
@@ -37,9 +41,9 @@ def find(key_name, key, value_name, options = {})
match[value_name].to_s
end
end
-
+
private
-
+
def normalize_for_comparison(string, options = {})
if options['sprintf']
if /\%[0-9\.]*f/.match options['sprintf']
@@ -49,6 +53,7 @@ def normalize_for_comparison(string, options = {})
end
string = sprintf % string
end
+ string.downcase! unless options['case_sensitive']
string.to_s.strip
end
end
View
2  test/support/countries.csv
@@ -0,0 +1,2 @@
+iso_3166_code,name
+DE,Germany
View
40 test/test_dictionary.rb
@@ -0,0 +1,40 @@
+$:.push File.dirname(__FILE__)
+require 'helper'
+
+class TestDataMinerDictionary < Test::Unit::TestCase
+ context 'case sensitive' do
+ setup do
+ options = {
+ :input => 'name',
+ :output => 'iso_3166_code',
+ :url => "file://#{File.expand_path ::File.dirname(__FILE__)}/support/countries.csv"
+ }
+ @dict = DataMiner::Dictionary.new DataMiner.recursively_stringify_keys(options)
+ end
+ should 'find "Germany"' do
+ assert_equal @dict.lookup("Germany"), 'DE'
+ end
+ should 'not find "GERMANY" or "germany"' do
+ assert_nil @dict.lookup("GERMANY")
+ assert_nil @dict.lookup("germany")
+ end
+ end
+ context 'case insensitive' do
+ setup do
+ options = {
+ :input => 'name',
+ :output => 'iso_3166_code',
+ :url => "file://#{File.expand_path ::File.dirname(__FILE__)}/support/countries.csv",
+ :case_insensitive => true
+ }
+ @dict = DataMiner::Dictionary.new DataMiner.recursively_stringify_keys(options)
+ end
+ should 'find "Germany"' do
+ assert_equal @dict.lookup("Germany"), 'DE'
+ end
+ should 'not also find "GERMANY" and "germany"' do
+ assert_equal @dict.lookup("GERMANY"), 'DE'
+ assert_equal @dict.lookup("germany"), 'DE'
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.