Permalink
Browse files

Added some documentation, changed fix_method to read as translate_to …

…so it makes a bit more sense
  • Loading branch information...
1 parent 4b62e02 commit d2cc212e71388414e87f23ccc577a56f771ec2af rrevans committed May 11, 2008
Showing with 38 additions and 16 deletions.
  1. +1 −1 Rakefile
  2. +3 −1 TODO
  3. +31 −11 lib/permalizer/permalink.rb
  4. +3 −3 spec/permalizer_spec.rb
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
@@ -4,4 +4,6 @@ Add Ruby 1.9 string encoding support for fun.
Remove ActiveSupport decompose string dependency
-Add documentation to the gem in RDoc format
+Add documentation to the gem in RDoc format
+
+Clean specs to reflect the changes in the destructive permalize! method
@@ -2,6 +2,7 @@
# Permalizer adds functionality to the String Object so that clean URLs creation is made simple
module Permalink
module InstanceMethods
+
# permalize! is a destructive method that will make the given string for use as a clean URL
# example:
# blog.title = "My Cool Gem!"
@@ -21,52 +22,71 @@ 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
+ # = Class variable which determines the Iconv fix method
+ # us_ascii is the default
+ @@translate_to = :us_ascii
@mapmarkus
mapmarkus May 11, 2008 Collaborator

Good change, it really makes more sense than “fix_method”

+
+ # = 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)
# 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 = word
decompose! if Permalizer.decompose_string && word.respond_to?(:chars) && word.chars.respond_to?(:decompose)
end
+ # translate to us_ascii
def us_ascii
Iconv.new('US-ASCII//TRANSLIT', 'UTF-8').iconv @word
end
+ # translate to utf-8
def utf_8
Iconv.new('UTF-8//TRANSLIT//IGNORE', 'UTF-8').iconv @word
end
+ # translate to iso_8859_1
def iso_8859_1
Iconv.new('ISO-8859-1//TRANSLIT//IGNORE', 'UTF-8').iconv @word
end
+
def to_s
- transform send(@@fix_method)
+ transform send(@@translate_to)
rescue
transform us_ascii
end
protected
- 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!
- @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 d2cc212

Please sign in to comment.