Permalink
Browse files

Merged from revans/master

  • Loading branch information...
2 parents 1a3c5fe + d2cc212 commit b5dfa638086ebd7d1903c302f195c953a54fc0a2 Marcos Arias committed May 11, 2008
Showing with 48 additions and 21 deletions.
  1. +6 −1 README
  2. +1 −1 Rakefile
  3. +7 −1 TODO
  4. +31 −15 lib/permalizer/permalink.rb
  5. +3 −3 spec/permalizer_spec.rb
View
@@ -4,4 +4,9 @@ Permalizer
Permalizer is a way to create clean URLs from strings. It is framework agnostic, meaning it’s simply just a gem that can be used in Rails, Merb, Sinatra, Camping, etc. It has two ways to use it, with the bang and without it:
“permalizer rocks!”.permalize # => “permalizer-rocks”
-“permalizer rocks # => “permalizer-rocks”
+“permalizer rocks # => “permalizer-rocks”
+
+Contributors
+------------
+
+Marcos Arias
View
@@ -3,7 +3,7 @@ require 'rake/gempackagetask'
PLUGIN = "permalizer"
NAME = "permalizer"
-VERSION = "0.2"
+VERSION = "0.4"
AUTHOR = "Robert R Evans"
EMAIL = "revans@robertrevans.com"
HOMEPAGE = "http://robertrevans.com"
View
@@ -1,3 +1,9 @@
Add in Support for more languages that iconv doesn't support.
-Add Ruby 1.9 string encoding support for fun.
+Add Ruby 1.9 string encoding support for fun.
+
+Remove ActiveSupport decompose string dependency
+
+Add documentation to the gem in RDoc format
+
+Clean specs to reflect the changes in the destructive permalize! method
@@ -62,15 +62,21 @@ def permalize
#
class Permalizer
- @@fix_method = :us_ascii
+ # = Class variable which determines the Iconv fix method
+ # us_ascii is the default
+ @@translate_to = :us_ascii
+ # = Class variable which determines whether the decompose hook is applied
+ # defaults to false
@@decompose_string = false
+
class << self
attr_accessor :decompose_string
- attr_accessor :fix_method
+ attr_accessor :translate_to
end
+
def initialize(word)
@word = word
decompose! if Permalizer.decompose_string && word.respond_to?(:chars) && word.chars.respond_to?(:decompose)
@@ -92,25 +98,35 @@ def iso_8859_1
Iconv.new('ISO-8859-1//TRANSLIT//IGNORE', 'UTF-8').iconv @word
end
- # Calls the fix_method and transforms the string passed in the constructor in a url valid string
+ # Calls the translate_to and transforms the string passed in the constructor in a url valid string
+
def to_s
- transform send(@@fix_method)
+ transform send(@@translate_to)
rescue
transform us_ascii
end
protected
-
- # Strips no url valid characters, transform spaces in dashes and down case the passed string
- def transform(word)
- word.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
- end
-
+
+ # = Transform
+ # Strips out unnecessary characters from any given word or phrase
+ #
+ # * first strips non word characters, replacing it with nothing
+ # * it keeps spaces and dashes, instead of replacing them with nothing
+ # * does a second pass of removing non-word characters, replacing it with a space
+ # * removes underscores, replacing it with a space
+ # * then splits the phrase into an array
+ # * rejoins the words with a dash
+ # * then downcases the phrase
+ def transform(word)
+ word.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
+ end
+
private
-
- def decompose! # :nodoc:
- @word = @word.chars.decompose
- end
-
+
+ def decompose!
+ @word = @word.chars.decompose
+ end
+
end
end
@@ -69,7 +69,7 @@
end
it "should correctly handle hyphens" do
- "here is a–hyphen".permalize.should eql("here-is-a-hyphen")
+ "here is a–hyphen test–this".permalize.should eql("here-is-a-hyphen-test-this")
"here is a–hyphen".permalize!.should eql("here-is-a-hyphen")
end
@@ -80,12 +80,12 @@
end
it "should perform permalization even with a misspelled fix_method" do
- Permalink::Permalizer.fix_method = :undefined_fix_method
+ Permalink::Permalizer.translate_to = :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
+ Permalink::Permalizer.translate_to = :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 b5dfa63

Please sign in to comment.