Permalink
Browse files

Use Ruby 2.1+ Refinements

  • Loading branch information...
carpodaster committed May 6, 2016
1 parent 75fc5e8 commit 319b4df5146313d31ad80dfd00d9e85056095191
Showing with 83 additions and 71 deletions.
  1. +58 −67 lib/string_normalizr.rb
  2. +24 −0 test/refinements_test.rb
  3. +1 −4 test/string_normalizr_test.rb
@@ -3,67 +3,63 @@
module AegisNet # :nodoc:
module StringNormalizr

COLLATION = {
'Ä' => 'Ae',
'Æ' => 'Ae',
'Å' => 'A',
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ç' => 'C',
'È' => "E",
'É' => "E",
'Ê' => "E",
'Ë' => 'E',
'Í' => 'I',
'Ì' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ñ' => 'N',
'Ö' => 'Oe',
'Œ' => 'Oe',
'Ø' => 'O',
'Ô' => 'O',
'Ó' => 'O',
'Ò' => 'O',
'Ü' => 'Ue',
'Ú' => 'U',
'Ù' => 'U',
'Ÿ' => 'Y',
'ä' => 'ae',
'æ' => 'ae',
'å' => 'a',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'í' => 'i',
'ì' => 'i',
'î' => 'i',
'ï' => 'i',
'ñ' => 'n',
'ö' => 'oe',
'œ' => 'oe',
'ø' => 'o',
'ô' => 'o',
'ó' => 'o',
'ò' => 'o',
'ü' => 'ue',
'ú' => 'u',
'ù' => 'u',
'ÿ' => 'y',
'ß' => 'ss',
}
refine String do

def self.included(base)
base.send(:include, InstanceMethods)
end

module InstanceMethods
COLLATION = {
'Ä' => 'Ae',
'Æ' => 'Ae',
'Å' => 'A',
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ç' => 'C',
'È' => "E",
'É' => "E",
'Ê' => "E",
'Ë' => 'E',
'Í' => 'I',
'Ì' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ñ' => 'N',
'Ö' => 'Oe',
'Œ' => 'Oe',
'Ø' => 'O',
'Ô' => 'O',
'Ó' => 'O',
'Ò' => 'O',
'Ü' => 'Ue',
'Ú' => 'U',
'Ù' => 'U',
'Ÿ' => 'Y',
'ä' => 'ae',
'æ' => 'ae',
'å' => 'a',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'í' => 'i',
'ì' => 'i',
'î' => 'i',
'ï' => 'i',
'ñ' => 'n',
'ö' => 'oe',
'œ' => 'oe',
'ø' => 'o',
'ô' => 'o',
'ó' => 'o',
'ò' => 'o',
'ü' => 'ue',
'ú' => 'u',
'ù' => 'u',
'ÿ' => 'y',
'ß' => 'ss',
}

# Returns a new String based on pre-defined normalization rules
#
@@ -106,16 +102,11 @@ def normalize(options = {})

# Performs the changes of AegisNet::StringNormalizr#normalize in place,
# returning the new string.
#
#
# See AegisNet::StringNormalizr#normalize for the optional parameter hash.
def normalize!(options = {})
self.replace(self.normalize(options))
replace normalize(options)
end

end
end
end

class String # :nodoc:
include AegisNet::StringNormalizr
end
@@ -0,0 +1,24 @@
require 'test_helper'

describe 'Normalization support using Ruby Refinements' do

subject { 'Öäß' }

describe 'with refinement' do
using AegisNet::StringNormalizr

it 'refines String' do
subject.normalize.must_equal 'Oeaess'
end
end

it 'leaves String untouched' do
begin
subject.normalize
assert false, 'subject should not respond to #normalize'
rescue => e
# this is what we want to test, actually:
e.must_be_kind_of NoMethodError
end
end
end
@@ -2,9 +2,7 @@

class StringNormalizrTest < MiniTest::Unit::TestCase

def test_string_integration
assert "some string".respond_to?(:normalize)
end
using AegisNet::StringNormalizr

def test_whitespaces
assert_equal "This-is-an-example", "This is an example".normalize
@@ -22,7 +20,6 @@ def test_downcase

def test_bang
foo = "some stríng"
assert foo.respond_to?(:normalize!)
foo.normalize!
assert_equal "some-string", foo
end

0 comments on commit 319b4df

Please sign in to comment.