Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Permalizer no longer tied to a framework

  • Loading branch information...
commit 3881038e79d8c7e4e44528593d2b95eda9ca461b 1 parent 94edf98
rrevans authored
View
5 init.rb
@@ -0,0 +1,5 @@
+require 'lib/permalizer'
+# mixin Permalizer in the String Object Class
+String.class_eval do
+ include Permalizer
+end
View
65 lib/permalizer.rb
@@ -1,42 +1,35 @@
-if defined?(Merb::Plugins)
- require 'iconv' # required for handling unicode/various languages
+require 'iconv' # required for handling unicode/various languages
- module Merb #:nodoc:
+# Permalizer adds functionality to the String Object so that clean URLs creation is made simple
+module Permalizer
- # Permalizer adds functionality to the String Object so that clean URLs creation is made simple
- module Permalizer
+ # permalize! is a destructive method that will make the given string for use as a clean URL
+ # <tt>example:</tt>
+ # blog.title = "My Cool Merb Plugin!"
+ # blog.title.permalize! # => "my-cool-merb-plugin"
+ #
+ def permalize!
+ permalink!(self)
+ end
- # permalize! is a destructive method that will make the given string for use as a clean URL
- # <tt>example:</tt>
- # blog.title = "My Cool Merb Plugin!"
- # blog.title.permalize! # => "my-cool-merb-plugin"
- #
- def permalize!
- permalink!(self)
- end
+ # permalize is the same as permalize! except that it is not a destructive method
+ # it creates a duplicate of the string and returns it as a clean string for URL usage
+ #
+ def permalize
+ string = self.dup
+ permalink!(string)
+ end
- # permalize is the same as permalize! except that it is not a destructive method
- # it creates a duplicate of the string and returns it as a clean string for URL usage
- #
- def permalize
- string = self.dup
- permalink!(string)
- end
+ private
- private
+ # permalink!
+ # Fix unicode characters, regex unwanted characters, split string, thus removing all whitespace, join it, and downcase it
+ #
+ def permalink!(word)
+ (Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv word).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
+ end
+end
- # permalink!
- # Fix unicode characters, regex unwanted characters, split string, thus removing all whitespace, join it, and downcase it
- #
- def permalink!(word)
- (Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv word).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
- end
- end
- end
-
- # mixin Permalizer in the String Object Class
- String.class_eval do
- include Merb::Permalizer
- end
-
-end
+String.class_eval do
+ include Permalizer
+end
View
36 spec/permalizer_spec.rb
@@ -1,42 +1,14 @@
require File.dirname(__FILE__) + '/spec_helper'
-MERB_PERMALIZER_ROOT = File.dirname(__FILE__) + "/.."
-# same structure as merb_helpers
+require File.dirname(__FILE__) + "/../lib/permalizer.rb"
describe "permalizer" do
- def unload_permalizer
- Merb.class_eval do
- remove_const("Permalizer") if defined?(Merb::Permalizer)
- end
- end
-
- def reload_permalizer
- unload_permalizer
- load(MERB_PERMALIZER_ROOT + "/lib/permalizer.rb")
- end
-
before(:each) do
- unload_permalizer
- end
-
- after(:all) do
- reload_permalizer
- end
-
- it "should not have permalizer" do
- unload_permalizer
- defined?(Merb::Permalizer).should be_nil
- end
-
- it "should load permalizer" do
- unload_permalizer
- reload_permalizer
- defined?(Merb::Permalizer).should_not be_nil
+ String.class_eval { include Permalizer }
end
- it "should load permalizer by default" do
- reload_permalizer
- defined?(Merb::Permalizer).should_not be_nil
+ it "should create a simple permalink" do
+ "testing one two three".permalize.should eql("testing-one-two-three")
end
end
View
8 spec/spec_helper.rb
@@ -1,10 +1,8 @@
-$TESTING=true
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
require 'rubygems'
-require 'merb'
-require 'merb/test/rspec'
+require 'spec'
Spec::Runner.configure do |config|
- config.include(Merb::Test::Helper)
- config.include(Merb::Test::RspecMatchers)
+ # config.include(Merb::Test::Helper)
+ # config.include(Merb::Test::RspecMatchers)
end
Please sign in to comment.
Something went wrong with that request. Please try again.