Skip to content

Commit 50cf8cd

Browse files
committed
Set up slug.rb for obvious method extractions.
1 parent 9daf53d commit 50cf8cd

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

lib/slug.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66
module Slug
77

88
def self.for(string)
9-
10-
str = string.dup
11-
str.gsub!(/^\s+|\s+$/, '')
12-
str.downcase!
13-
9+
str = string.dup.strip.downcase
10+
1411
# The characters we want to replace with a hyphen
1512
str.tr!("·/_,:;.", "\-")
1613

1714
# Convert to ASCII or remove if transliteration is unknown.
1815
str = ActiveSupport::Inflector.transliterate(str, '')
19-
16+
17+
# Remove everything except alphanumberic, space, and hyphen characters.
2018
str.gsub!(/[^a-z0-9 -]/, '')
19+
20+
# Replace multiple spaces with one hyphen.
2121
str.gsub!(/\s+/, '-')
22+
23+
# Replace multiple hyphens with one hyphen.
2224
str.gsub!(/\-+/, '-')
25+
26+
# Remove leading and trailing hyphens
2327
str.gsub!(/^-|-$/, '')
2428

2529
str

spec/components/slug_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# encoding: utf-8
22

33
require 'spec_helper'
4-
54
require 'slug'
65

76
describe Slug do
87

9-
108
it 'replaces spaces with hyphens' do
119
Slug.for("hello world").should == 'hello-world'
1210
end

0 commit comments

Comments
 (0)