slugity 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/slugity/extend_string.rb +5 -2
- data/lib/slugity/matchers.rb +34 -0
- data/lib/slugity/utilities.rb +2 -2
- data/lib/slugity/version.rb +1 -1
- data/lib/slugity.rb +11 -3
- data/spec/slugity/matchers_spec.rb +35 -0
- data/spec/slugity/trim_string_spec.rb +1 -1
- data/spec/slugity_spec.rb +7 -1
- data/spec/string_spec.rb +6 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzA0NGY5YTY3YTBjZDc4MjE5YTgzNTJiZTQ5YzQ1ZDM0NjRjODkwOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NDRkZjhhMmMxMTFmY2E1MjU3ZTYwZDdkY2QzNTg1YzJkZDI5OTc5NQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzkyNGI1MDk1ZmMxMzlmYzU4MGE4OTNmNmJhY2E3OGUxYjM3MTQyMGE1MmJj
|
10
|
+
MWRjMjkzOGIyZmZjOWUwMmUyMWE0NjAxNTlhZmJiY2JjMzdiMWIxY2FiNDc5
|
11
|
+
MWUyYjQ1NGRhNDJhZDliY2RjY2RmYmIzNTFjNDg2ZjkyMzNkNDk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzhlMDlhZDZhM2EwYzBmYmZmMzlmYWYxZGNjNjI0ZGIxZDIzM2M2NGIzMDFm
|
14
|
+
NGIxOThmYzk3ZDgwYTYzNmFlY2I5NTNlY2VlNjVhOGI1ZjZlMWEzYWQ1MTU4
|
15
|
+
M2RiMTJiYTRkMjI4ZDU3YjI2ZDA1ZjdlOWI1NTg4MDY3ZWU3Y2Q=
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Slugity
|
2
|
+
module Matchers
|
3
|
+
|
4
|
+
@@matchers = {
|
5
|
+
# define the default matchers
|
6
|
+
default: {
|
7
|
+
/\s|\// => '-',
|
8
|
+
/\.|\'|\"|\<|\>|\,|\(|\)|\:/ => '',
|
9
|
+
/\&/ => 'and',
|
10
|
+
/\+/ => 'plus',
|
11
|
+
/\=/ => 'equals'
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
# fetch a matcher
|
16
|
+
# @param key [Symbol]
|
17
|
+
# @return [Hash]
|
18
|
+
def self.use key
|
19
|
+
@@matchers[key]
|
20
|
+
end
|
21
|
+
|
22
|
+
# add a matcher hash
|
23
|
+
# @param key [Symbol]
|
24
|
+
# @param use_default [Boolean]
|
25
|
+
# @param options [Hash]
|
26
|
+
def self.add key, use_default=true, options={}
|
27
|
+
if use_default
|
28
|
+
options = @@matchers[:default].merge(options)
|
29
|
+
end
|
30
|
+
@@matchers[key] = options
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/slugity/utilities.rb
CHANGED
data/lib/slugity/version.rb
CHANGED
data/lib/slugity.rb
CHANGED
@@ -1,15 +1,23 @@
|
|
1
1
|
require 'slugity/utilities'
|
2
|
+
require 'slugity/matchers'
|
2
3
|
|
3
4
|
module Slugity
|
4
5
|
|
5
6
|
# Converts the given string into a slug
|
6
7
|
#
|
7
8
|
# @param string [String] the string to slugity
|
9
|
+
# @param matcher [Symbol] the matcher to use
|
8
10
|
# @return [String] the slug version of the provided string
|
9
|
-
def slugity string
|
10
|
-
string = Util.trim_string( string )
|
11
|
+
def slugity string, matcher=:default
|
12
|
+
string = Util.trim_string( string ).downcase
|
11
13
|
|
12
|
-
|
14
|
+
Slugity::Matchers.use(matcher).each do |match,replacement|
|
15
|
+
string.gsub!( match, replacement )
|
16
|
+
end
|
17
|
+
|
18
|
+
string.gsub!( /[^a-z0-9\-\_]/, '' )
|
19
|
+
|
20
|
+
return string
|
13
21
|
end
|
14
22
|
|
15
23
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'slugity'
|
3
|
+
|
4
|
+
describe Slugity::Matchers do
|
5
|
+
include Slugity
|
6
|
+
|
7
|
+
describe '#default_matchers' do
|
8
|
+
|
9
|
+
it 'returns a hash table' do
|
10
|
+
Slugity::Matchers.use(:default).class.should == Hash
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#set_matcher' do
|
16
|
+
|
17
|
+
it 'allows custom matchers to be set' do
|
18
|
+
Slugity::Matchers.add :percent, true, {
|
19
|
+
/\%/ => 'percent'
|
20
|
+
}
|
21
|
+
|
22
|
+
slugity( "hello 20% of the world", :percent ).should == "hello-20percent-of-the-world"
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'allows matchers to be overwritten' do
|
27
|
+
Slugity::Matchers.add :plus, true, {
|
28
|
+
/\+/ => 'not-plus'
|
29
|
+
}
|
30
|
+
|
31
|
+
slugity( "one + one", :plus ).should == "one-not-plus-one"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/spec/slugity_spec.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
|
-
require
|
4
|
+
require 'slugity'
|
3
5
|
|
4
6
|
test_strings = {
|
5
7
|
"Hello World" => "hello-world",
|
@@ -21,6 +23,10 @@ describe Slugity do
|
|
21
23
|
slugity( input ).should == output
|
22
24
|
end
|
23
25
|
end
|
26
|
+
|
27
|
+
it "strips unrecognized characters" do
|
28
|
+
slugity( "aëòúi".encode("UTF-8") ).should == "ai"
|
29
|
+
end
|
24
30
|
end
|
25
31
|
|
26
32
|
end
|
data/spec/string_spec.rb
CHANGED
@@ -7,6 +7,12 @@ describe String do
|
|
7
7
|
it "should have been included on String" do
|
8
8
|
"hello world".to_slug.should == "hello-world"
|
9
9
|
end
|
10
|
+
it "lets you pass custom matchers" do
|
11
|
+
Slugity::Matchers.add :plus, true, {
|
12
|
+
/\+/ => 'not-plus'
|
13
|
+
}
|
14
|
+
"one + one".to_slug(:plus).should == "one-not-plus-one"
|
15
|
+
end
|
10
16
|
end
|
11
17
|
|
12
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slugity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Sloan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ! ' Yet another slugging gem, this one makes sure you want to extend
|
14
14
|
the String class instead of assuming. '
|
@@ -18,9 +18,11 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/slugity/extend_string.rb
|
21
|
+
- lib/slugity/matchers.rb
|
21
22
|
- lib/slugity/utilities.rb
|
22
23
|
- lib/slugity/version.rb
|
23
24
|
- lib/slugity.rb
|
25
|
+
- spec/slugity/matchers_spec.rb
|
24
26
|
- spec/slugity/trim_string_spec.rb
|
25
27
|
- spec/slugity_spec.rb
|
26
28
|
- spec/spec_helper.rb
|
@@ -44,12 +46,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
46
|
version: '0'
|
45
47
|
requirements: []
|
46
48
|
rubyforge_project:
|
47
|
-
rubygems_version: 2.0.
|
49
|
+
rubygems_version: 2.0.3
|
48
50
|
signing_key:
|
49
51
|
specification_version: 4
|
50
52
|
summary: Yet another slugging gem
|
51
53
|
test_files:
|
54
|
+
- spec/slugity/matchers_spec.rb
|
52
55
|
- spec/slugity/trim_string_spec.rb
|
53
56
|
- spec/slugity_spec.rb
|
54
57
|
- spec/spec_helper.rb
|
55
58
|
- spec/string_spec.rb
|
59
|
+
has_rdoc:
|