Browse files

Added more Iconv conversion methods. Added extension capavility. Adde…

…d some specs to cover the changes.
  • Loading branch information...
1 parent 87e897f commit ea4278323c898ff82b5cda6ee68e658cd78424b7 Marcos Arias committed May 2, 2008
Showing with 51 additions and 8 deletions.
  1. +36 −8 lib/permalizer/permalink.rb
  2. +15 −0 spec/permalizer_spec.rb
View
44 lib/permalizer/permalink.rb
@@ -8,7 +8,7 @@ module InstanceMethods
# blog.title.permalize! # => "my-cool-gem"
#
def permalize!
- replace Permalink::Permalizer.new(self).to_s
+ replace permalize
end
# permalize is the same as permalize! except that it is not a destructive method
@@ -21,24 +21,52 @@ def permalize
class Permalizer
+ # Class variable which determines the Iconv fix method
+ @@fix_method = :us_ascii
+ # Class variable which determines whether the decompose hook is applied
+ @@decompose_string = false
- # This allow to set Permalink::Permalizer.decompose_string = true in a initializer hook
class << self
attr_accessor :decompose_string
+ attr_accessor :fix_method
end
def initialize(word)
# only if decompose_string is true and ActiveSupport is loaed in UTF-8 mode (with UTF8Handler as handler class)
# This is required for some remote Linux machines, I regulary use it in production environment
- @word = (Permalizer.decompose_string && word.respond_to?(:chars) && word.chars.respond_to?(:decompose))? word.chars.decompose : word
+ @word = word
+ decompose! if Permalizer.decompose_string && word.respond_to?(:chars) && word.chars.respond_to?(:decompose)
+ end
+
+ def us_ascii
+ Iconv.new('US-ASCII//TRANSLIT', 'UTF-8').iconv @word
+ end
+
+ def utf_8
+ Iconv.new('UTF-8//TRANSLIT//IGNORE', 'UTF-8').iconv @word
+ end
+
+ def iso_8859_1
+ Iconv.new('ISO-8859-1//TRANSLIT//IGNORE', 'UTF-8').iconv @word
end
- # permalink
- # Fix unicode characters, regex unwanted characters, split string, thus removing all whitespace, join it, and downcase it
- #
- # TODO: Ruby 1.9 string encoding
def to_s
- (Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv @word).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
+ transform send(@@fix_method)
+ rescue
+ transform us_ascii
end
+
+ protected
+
+ def transform(word)
+ word.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
+ end
+
+ private
+
+ def decompose!
+ @word = @word.chars.decompose
+ end
+
end
end
View
15 spec/permalizer_spec.rb
@@ -16,6 +16,12 @@
"I've been banged".permalize!.should eql("ive-been-banged")
end
+ it "should ovewrite string original value when permalize! is called" do
+ string = "i'll be overriden soon"
+ string.permalize!
+ string.should eql("ill-be-overriden-soon")
+ end
+
it "should correctly handle single and double quotes" do
"\'here are some single quotes\'".permalize.should eql("here-are-some-single-quotes")
"\'here are some single quotes\'".permalize!.should eql("here-are-some-single-quotes")
@@ -73,4 +79,13 @@
"Should HanDle UPPER case LeTtErS".permalize!.should eql("should-handle-upper-case-letters")
end
+ it "should perform permalization even with a misspelled fix_method" do
+ Permalink::Permalizer.fix_method = :undefined_fix_method
+ "This will be permalized".permalize.should eql("this-will-be-permalized")
+ end
+
+ it "should perform an utf-8 transformation" do
+ Permalink::Permalizer.fix_method = :utf_8
+ "This string contains utf-8 characters: esdrújula. €100".permalize.should eql("this-string-contains-utf-8-characters-esdrujula-eur100")
+ end
end

0 comments on commit ea42783

Please sign in to comment.