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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzdkYjE4NjdiYTA4ZTY0MDVhYjMwZDkzMWY5ZmNlNTE0NThjY2RiNA==
4
+ NzA0NGY5YTY3YTBjZDc4MjE5YTgzNTJiZTQ5YzQ1ZDM0NjRjODkwOA==
5
5
  data.tar.gz: !binary |-
6
- ZDZjZWE0YTY0M2M5Yjc4Y2JjNWY3YWVhYjI5MGUwYjljZTU5MTMyMg==
6
+ NDRkZjhhMmMxMTFmY2E1MjU3ZTYwZDdkY2QzNTg1YzJkZDI5OTc5NQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NGY3Y2QyNzI0MjgyMjdkNGEzNTBkZDQzY2MzOTBkMTk1ZmZhNTFiMzI3MDkw
10
- YmE2N2RkYWYzNTBmOWEzNWZiNGM5MTUzOWEwYjgzNjkxNDBkZjUyYmY4MjVi
11
- YTQzNzVjNTQ1ZjBhYWI4OTRjZmM2YmYwMzlkNGRhMWU4MGE1Y2M=
9
+ NzkyNGI1MDk1ZmMxMzlmYzU4MGE4OTNmNmJhY2E3OGUxYjM3MTQyMGE1MmJj
10
+ MWRjMjkzOGIyZmZjOWUwMmUyMWE0NjAxNTlhZmJiY2JjMzdiMWIxY2FiNDc5
11
+ MWUyYjQ1NGRhNDJhZDliY2RjY2RmYmIzNTFjNDg2ZjkyMzNkNDk=
12
12
  data.tar.gz: !binary |-
13
- NTFjOGIzNjNlZWQzYWJiMjQwNzk3NDBlMDQ5MDJiZGI4MTc0ZWIwMTJiMDZh
14
- Y2M2YmEzYzRmMTAwMzFhNWU5NTk3M2VjZDU1M2IxNGZjNWIzM2VlODdlNGM2
15
- YWNhYjM1ZDM1NjI2MmU1MzIzMzFkZmNhOWM2ZWRjMTE3NTk0ZTA=
13
+ NzhlMDlhZDZhM2EwYzBmYmZmMzlmYWYxZGNjNjI0ZGIxZDIzM2M2NGIzMDFm
14
+ NGIxOThmYzk3ZDgwYTYzNmFlY2I5NTNlY2VlNjVhOGI1ZjZlMWEzYWQ1MTU4
15
+ M2RiMTJiYTRkMjI4ZDU3YjI2ZDA1ZjdlOWI1NTg4MDY3ZWU3Y2Q=
@@ -3,8 +3,11 @@ require 'slugity'
3
3
  class String
4
4
  include Slugity
5
5
 
6
- def to_slug
7
- slugity self
6
+ # call slugity on self
7
+ # @param key [Symbol] the matcher to use
8
+ # @return [String] the slugged string
9
+ def to_slug key=:default
10
+ slugity self, key
8
11
  end
9
12
 
10
13
  end
@@ -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
@@ -11,11 +11,11 @@ module Slugity
11
11
 
12
12
  # capture spaces at the begining or end of the string
13
13
  pattern = /(^\s+|\s+$)/
14
-
14
+
15
15
  # strip characters that match the pattern
16
16
  string.gsub( pattern, '' )
17
17
  end
18
-
18
+
19
19
  end
20
20
 
21
21
  end
@@ -1,3 +1,3 @@
1
1
  module Slugity
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
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
- string.downcase.gsub(/\s|\//, '-').gsub(/\.|\'|\"|\<|\>|,|\(|\)|\:/,'').gsub(/\&/,'and').gsub(/\+/,'plus').gsub(/\=/,'equals')
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
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require File.expand_path("../../../lib/slugity/utilities", __FILE__)
2
+ require 'slugity/utilities'
3
3
 
4
4
  describe Slugity::Util do
5
5
 
data/spec/slugity_spec.rb CHANGED
@@ -1,5 +1,7 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
- require File.expand_path("../../lib/slugity", __FILE__)
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.1
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 00:00:00.000000000 Z
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.2
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: