agnostic_slugs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 60d42bf414ee63ea0fff3b603f39d81ef623c055
4
+ data.tar.gz: aec643b42421f40dc26b1757c848018ca86639de
5
+ SHA512:
6
+ metadata.gz: 800a7886f1002f94b05c1edbf4af3feae6c9d05b563457cc5745d6906ff17650cf91557811eee6228914e44f5ff2403f123bb8da967f670d94206d649035fd47
7
+ data.tar.gz: 36a809879ccc72f27a10fd687fc77b56a9a7d806b9fed663be77afe681179514d2616dc7bb8bbcdb9aadf64d0029603c14a400f337d4f1d153f2a0e37b7ed091
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,3 @@
1
+ # 0.0.1
2
+
3
+ Initial version with basic functionality
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in agnostic_slugs.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Lasse Skindstad Ebert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # AgnosticSlugs
2
+
3
+ agnostic_slugs is a simple slug generator that is agnostic.
4
+
5
+ * It does not care about ORMs.
6
+ * It will not monkey patch
7
+
8
+ It can create slugs. Period.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'agnostic_slugs'
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ slug = AgnosticSlugs::Slug.new('Look at my pretty new shoes! :)')
21
+ slug.to_s # => "look-at-my-pretty-new-shoes"
22
+ slug.next.to_s # => "look-at-my-pretty-new-shoes-2"
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/[my-github-username]/agnostic_slugs/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'agnostic_slugs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "agnostic_slugs"
8
+ spec.version = AgnosticSlugs::VERSION
9
+ spec.authors = ["Lasse Skindstad Ebert"]
10
+ spec.email = ["lasse@lasseebert.dk"]
11
+ spec.summary = %q{Slug generator that is agnostic about ORMs}
12
+ spec.homepage = "https://github.com/lasseebert/agnostic_slugs"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.7'
21
+ spec.add_development_dependency 'pry-byebug'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
24
+ end
@@ -0,0 +1,5 @@
1
+ require 'agnostic_slugs/slug'
2
+ require 'agnostic_slugs/version'
3
+
4
+ module AgnosticSlugs
5
+ end
@@ -0,0 +1,111 @@
1
+ module AgnosticSlugs
2
+ class Slug
3
+ attr_reader :original, :counter
4
+
5
+ def initialize(original, counter = 1)
6
+ @original = original
7
+ @counter = counter
8
+ end
9
+
10
+ def next
11
+ self.class.new(original, counter + 1)
12
+ end
13
+
14
+ def to_s
15
+ @slug ||= generate
16
+ end
17
+
18
+ private
19
+
20
+ def generate
21
+ result = original
22
+ result = replace_accents(result)
23
+ result = result.gsub(/[\s_\-]+/, '-')
24
+ result = result.downcase
25
+ result = result.gsub(/[^a-z0-9\-]/, '')
26
+
27
+ if counter > 1
28
+ "#{result}-#{counter}"
29
+ else
30
+ result
31
+ end
32
+ end
33
+
34
+ def replace_accents(string)
35
+ # This was taken directly from https://github.com/ericboehs/to_slug
36
+ # Thanks, ericboehs :)
37
+
38
+ # Define which accents map to which ascii characters
39
+ accents = {
40
+ # Uppercase
41
+ 'A' => %w(À Á Â Ã Ā Ă Ȧ Ả Ä Å Ǎ Ȁ Ȃ Ą Ạ Ḁ Ầ Ấ Ẫ Ẩ Ằ Ắ Ẵ Ẳ Ǡ Ǟ Ǻ Ậ Ặ),
42
+ 'AE' => %w(Æ Ǽ Ǣ),
43
+ 'B' => %w(Ḃ Ɓ Ḅ Ḇ Ƃ Ƅ),
44
+ 'C' => %w(C Ć Ĉ Ċ Č Ƈ Ç Ḉ),
45
+ 'D' => %w(D Ḋ Ɗ Ḍ Ḏ Ḑ Ḓ Ď Đ Ɖ Ƌ),
46
+ 'E' => %w(È É Ê Ẽ Ē Ĕ Ė Ë Ẻ Ě Ȅ Ȇ Ẹ Ȩ Ę Ḙ Ḛ Ề Ế Ễ Ể Ḕ Ḗ Ệ Ḝ Ǝ Ɛ),
47
+ 'F' => %w(Ḟ Ƒ),
48
+ 'G' => %w(Ǵ Ĝ Ḡ Ğ Ġ Ǧ Ɠ Ģ Ǥ),
49
+ 'H' => %w(Ĥ Ḣ Ḧ Ȟ Ƕ Ḥ Ḩ Ḫ Ħ),
50
+ 'I' => %w(Ì Í Î Ĩ Ī Ĭ İ Ï Ỉ Ǐ Ị Į Ȉ Ȋ Ḭ Ɨ Ḯ),
51
+ 'IJ' => %w(IJ),
52
+ 'J' => %w(Ĵ),
53
+ 'K' => %w(Ḱ Ǩ Ḵ Ƙ Ḳ Ķ),
54
+ 'L' => %w(Ĺ Ḻ Ḷ Ļ Ḽ Ľ Ŀ Ł Ḹ),
55
+ 'M' => %w(Ḿ Ṁ Ṃ Ɯ),
56
+ 'N' => %w(Ǹ Ń Ñ Ṅ Ň Ŋ Ɲ Ṇ Ņ Ṋ Ṉ Ƞ),
57
+ 'O' => %w(Ò Ó Ô Õ Ō Ŏ Ȯ Ö Ỏ Ő Ǒ Ȍ Ȏ Ơ Ǫ Ọ Ɵ Ø Ồ Ố Ỗ Ổ Ȱ Ȫ Ȭ Ṍ Ṏ Ṑ Ṓ Ờ Ớ Ỡ Ở Ợ Ǭ Ộ Ǿ Ɔ),
58
+ 'OE' => %w(Œ),
59
+ 'P' => %w(Ṕ Ṗ Ƥ),
60
+ 'R' => %w(Ŕ Ṙ Ř Ȑ Ȓ Ṛ Ŗ Ṟ Ṝ Ʀ),
61
+ 'S' => %w(Ś Ŝ Ṡ Š Ṣ Ș Ş Ṥ Ṧ Ṩ),
62
+ 'T' => %w(Ṫ Ť Ƭ Ʈ Ṭ Ț Ţ Ṱ Ṯ Ŧ),
63
+ 'U' => %w(Ù Ú Û Ũ Ū Ŭ Ü Ủ Ů Ű Ǔ Ȕ Ȗ Ư Ụ Ṳ Ų Ṷ Ṵ Ṹ Ṻ Ǜ Ǘ Ǖ Ǚ Ừ Ứ Ữ Ử Ự),
64
+ 'V' => %w(Ṽ Ṿ Ʋ),
65
+ 'W' => %w(Ẁ Ẃ Ŵ Ẇ Ẅ Ẉ),
66
+ 'X' => %w(Ẋ Ẍ),
67
+ 'Y' => %w(Ỳ Ý Ŷ Ỹ Ȳ Ẏ Ÿ Ỷ Ƴ Ỵ),
68
+ 'Z' => %w(Ź Ẑ Ż Ž Ȥ Ẓ Ẕ Ƶ),
69
+
70
+ # Lowercase
71
+ 'a' => %w(à á â ã ā ă ȧ ä ả å ǎ ȁ ȃ ą ạ ḁ ẚ ầ ấ ẫ ẩ ằ ắ ẵ ẳ ǡ ǟ ǻ ậ ặ),
72
+ 'ae' => %w(æ ǽ ǣ),
73
+ 'b' => %w(ḃ ɓ ḅ ḇ ƀ ƃ ƅ),
74
+ 'c' => %w(ć ĉ ċ č ƈ ç ḉ),
75
+ 'd' => %w(ḋ ɗ ḍ ḏ ḑ ḓ ď đ ƌ ȡ),
76
+ 'e' => %w(è é ê ẽ ē ĕ ė ë ẻ ě ȅ ȇ ẹ ȩ ę ḙ ḛ ề ế ễ ể ḕ ḗ ệ ḝ ɛ),
77
+ 'f' => %w(ḟ ƒ),
78
+ 'g' => %w(ǵ ĝ ḡ ğ ġ ǧ ɠ ģ ǥ),
79
+ 'h' => %w(ĥ ḣ ḧ ȟ ƕ ḩ ḫ ẖ ħ),
80
+ 'i' => %w(ì í î ĩ ī ĭ ı ï ỉ ǐ į į ȋ ḭ ɨ ḯ),
81
+ 'ij' => %w(ij),
82
+ 'j' => %w(ĵ ǰ),
83
+ 'k' => %w(ḱ ǩ ƙ ḳ ķ),
84
+ 'l' => %w(ĺ ḻ ḷ ļ ḽ ľ ŀ ł ƚ ḹ ȴ),
85
+ 'm' => %w(ḿ ṁ ṃ ɯ),
86
+ 'n' => %w(ǹ ń ñ ṅ ň ŋ ɲ ṇ ņ ṋ ṉ ʼn ȵ),
87
+ 'o' => %w(ò ó ô õ ō ŏ ȯ ö ỏ ő ǒ ȍ ȏ ơ ǫ ọ ɵ ø ồ ố ỗ ổ ȱ ȫ ȭ ṍ ṏ ṑ ṓ ờ ớ ỡ ở ợ ǭ ộ ǿ ɔ),
88
+ 'oe' => %w(œ),
89
+ 'p' => %w(ṕ ṗ ƥ),
90
+ 'r' => %w(ŕ ṙ ř ȑ ȓ ṛ ŗ ṟ ṝ),
91
+ 's' => %w(ś ŝ ṡ š ṣ ș ş ṥ ṧ ṩ ß ſ ẛ),
92
+ 't' => %w(ṫ ẗ ť ƭ ʈ ƫ ṭ ț ţ ṱ ṯ ŧ ȶ),
93
+ 'u' => %w(ù ú û ũ ū ŭ ü ủ ů ű ǔ ȕ ȗ ư ụ ṳ ų ṷ ṵ ṹ ṻ ǖ ǜ ǘ ǖ ǚ ừ ứ ữ ử ự),
94
+ 'v' => %w(ṽ ṿ),
95
+ 'w' => %w(ẁ ẃ ŵ ẇ ẅ ẘ ẉ),
96
+ 'x' => %w(ẋ ẍ),
97
+ 'y' => %w(ỳ ý ŷ ỹ ȳ ẏ ÿ ỷ ẙ ƴ ỵ),
98
+ 'z' => %w(ź ẑ ż ž ȥ ẓ ẕ ƶ),
99
+
100
+ # Not sure what to do with these
101
+ '' => %w(Ð Þ Ə Ɣ Ɩ Ƣ Ƨ Ʃ Ʊ Ʒ Ǯ Ƹ Ȝ ƿ Ȣ ð þ ə ɣ ɩ ƣ ƨ ʃ ƪ ʊ ʒ ǯ ƹ ƺ ȝ Ƿ ȣ DZ Dz dz DŽ Dž dž LJ Lj lj NJ Nj nj ĸ ƍ ƛ ƾ ƻ Ƽ ƽ)
102
+ }
103
+
104
+ accents.each do |replacement, accent|
105
+ regex = Regexp.new("[#{accent.join("|")}]")
106
+ string = string.gsub(regex, replacement)
107
+ end
108
+ string
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,3 @@
1
+ module AgnosticSlugs
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'pry-byebug'
2
+
3
+ RSpec.configure do |config|
4
+ config.filter_run :focus
5
+ config.run_all_when_everything_filtered = true
6
+
7
+ config.order = :random
8
+
9
+ config.expect_with :rspec do |expectations|
10
+ expectations.syntax = :expect
11
+ end
12
+
13
+ config.mock_with :rspec do |mocks|
14
+ mocks.syntax = :expect
15
+
16
+ mocks.verify_partial_doubles = true
17
+ end
18
+ end
@@ -0,0 +1,46 @@
1
+ require 'agnostic_slugs/slug'
2
+
3
+ module AgnosticSlugs
4
+ describe Slug do
5
+ describe '#to_s' do
6
+
7
+ subject { Slug.new(input, counter).to_s }
8
+ let(:counter) { 1 }
9
+
10
+ describe 'simple input' do
11
+ let(:input) { 'Simple string' }
12
+ it { should eq('simple-string') }
13
+ end
14
+
15
+ describe 'accented input' do
16
+ let(:input) { 'Hellø Wårld' }
17
+ it { should eq('hello-warld') }
18
+ end
19
+
20
+ describe 'punctuations' do
21
+ let(:input) { 'Hello world!' }
22
+ it { should eq('hello-world') }
23
+ end
24
+
25
+ describe 'with a counter' do
26
+ let(:input) { 'hello-world' }
27
+ let(:counter) { 2 }
28
+ it { should eq('hello-world-2') }
29
+ end
30
+ end
31
+
32
+ describe '#next' do
33
+ it 'should increment counter' do
34
+ actual = Slug.new('foo').next.counter
35
+ expect(actual).to eq(2)
36
+ end
37
+
38
+ it 'should be immutable' do
39
+ slug = Slug.new('foo')
40
+ next_slug = slug.next
41
+
42
+ expect(slug.counter).to eq(1)
43
+ end
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agnostic_slugs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lasse Skindstad Ebert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ description:
70
+ email:
71
+ - lasse@lasseebert.dk
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - agnostic_slugs.gemspec
84
+ - lib/agnostic_slugs.rb
85
+ - lib/agnostic_slugs/slug.rb
86
+ - lib/agnostic_slugs/version.rb
87
+ - spec/spec_helper.rb
88
+ - spec/unit/slug_spec.rb
89
+ homepage: https://github.com/lasseebert/agnostic_slugs
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Slug generator that is agnostic about ORMs
113
+ test_files:
114
+ - spec/spec_helper.rb
115
+ - spec/unit/slug_spec.rb