slug_fu 1.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/lib/slug_fu.rb +37 -0
- data/lib/slug_fu/default_naming_strategy.rb +22 -0
- data/lib/slug_fu/model_context.rb +20 -0
- data/lib/slug_fu/version.rb +4 -0
- data/slug_fu.gemspec +26 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7b2b860388aa2f04e298622c041a7cdb2a74eca6
|
4
|
+
data.tar.gz: fd1024cf2d9a19e7692e329e31c3683f12e000bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86a6e691ed16991366d7963929190c691da0b8cfedf9caf60935678f8383f2b028a9b60e4b6419174daf9bffd735ac70dfe95ef4c5eda7a4366a1ef717d69115
|
7
|
+
data.tar.gz: 35908b4ea2a0303645369a552c87795f22e07b8d5c482c62a69ccfa7f36a87509da774817324f87b5c95e37adc0f77f949879537c2eea121f63d5a48f85df6be
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Bradley Schaefer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# SlugFu
|
2
|
+
|
3
|
+
Yet Another gem for generating slug strings suitable for URLs (or whatever your slug business may be).
|
4
|
+
|
5
|
+
**What makes this one different?** This gives you tools for ensuring the uniqueness of your slugs,
|
6
|
+
and if your slug is not unique it allows you to plug in different strategies for generating unique slugs.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'slug_fu'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install slug_fu
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
include SlugFu
|
28
|
+
|
29
|
+
SlugFu(string) # doesn't need to check uniqueness, just make a slug string
|
30
|
+
|
31
|
+
SlugFu(string, context: %w(one two)) # generated slug will unique as far as `context.include?(slug)` is concerned
|
32
|
+
|
33
|
+
class NamingStrategy
|
34
|
+
def initialize(str)
|
35
|
+
@str = str
|
36
|
+
end
|
37
|
+
|
38
|
+
def next
|
39
|
+
@next = @next.nil? ? @str : @next + "-"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
SlugFu("one", context: %w(one one-), naming_strategy: NamingStrategy) # Use a custom strategy for naming, calling #next until a unique name is found
|
43
|
+
```
|
44
|
+
|
45
|
+
### Usage with Rails
|
46
|
+
|
47
|
+
SlugFu supplies `SlugFu::ModelContext` for ensuring uniqueness on Rails models.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
include SlugFu
|
51
|
+
|
52
|
+
SlugFu(str, context: SlugFu::ModelContext.new(Book)) # slug will be unique for the `Book#slug` attribute
|
53
|
+
SlugFu(str, context: SlugFu::ModelContext.new(Book.where(language: "en")) # slug will be unique for the `Book#slug` attribute in the given scope
|
54
|
+
SlugFu(str, context: SlugFu::ModelContext.new(Book, :url_slug)) # slug will be unique for the `Book#url_slug` attribute
|
55
|
+
```
|
56
|
+
|
57
|
+
## Development
|
58
|
+
|
59
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/soulcutter/slug_fu
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/slug_fu.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "singleton"
|
3
|
+
|
4
|
+
require "slug_fu/version"
|
5
|
+
require "slug_fu/default_naming_strategy"
|
6
|
+
require "slug_fu/model_context"
|
7
|
+
|
8
|
+
module SlugFu
|
9
|
+
def SlugFu(str, context: NoContext.instance, naming_strategy: DefaultNamingStrategy)
|
10
|
+
namer = naming_strategy.new(::SlugFu.slugify(str))
|
11
|
+
|
12
|
+
begin
|
13
|
+
result = namer.next
|
14
|
+
end while context.include?(result)
|
15
|
+
result
|
16
|
+
end
|
17
|
+
module_function :SlugFu
|
18
|
+
|
19
|
+
def self.slugify(str)
|
20
|
+
str.to_s.gsub(/^\s\s*/, "") # Trim left
|
21
|
+
.gsub(/\s\s*$/, "") # Trim right
|
22
|
+
.gsub(/\_/, "-") # Convert underscores to hyphens
|
23
|
+
.gsub(/&/, " and ") # Replace ampersand with " and "
|
24
|
+
.gsub(/\s+/, "-") # Replace extra spaces with only one hyphen
|
25
|
+
.gsub(/[^\w\-\u00E0-\u00FC]/, "") # Strip everything but alphanumeric, hypen, and accented characters
|
26
|
+
.gsub(/\-+/, "-") # Replace multiple hypens with one
|
27
|
+
.downcase
|
28
|
+
end
|
29
|
+
|
30
|
+
class NoContext
|
31
|
+
include Singleton
|
32
|
+
|
33
|
+
def include?(*)
|
34
|
+
false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module SlugFu
|
3
|
+
# This strategy tries the original slug first, then appends increasing
|
4
|
+
# numbers. The sequence would look like so:
|
5
|
+
#
|
6
|
+
# "slug"
|
7
|
+
# "slug-1"
|
8
|
+
# "slug-2"
|
9
|
+
# "slug-3"
|
10
|
+
class DefaultNamingStrategy
|
11
|
+
def initialize(str)
|
12
|
+
@str = str
|
13
|
+
@count = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def next
|
17
|
+
slug = @count > 0 ? "#{@str}-#{@count}" : @str
|
18
|
+
@count += 1
|
19
|
+
slug
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module SlugFu
|
3
|
+
# Context that can be used with ActiveRecord models to check if a slug
|
4
|
+
# already exists in a given scope.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# SlugFu(str, context: SlugFu::ModelContext.new(Book, :url_slug))
|
9
|
+
class ModelContext
|
10
|
+
def initialize(scope, attr = :slug)
|
11
|
+
@scope = scope
|
12
|
+
@attr = attr
|
13
|
+
freeze
|
14
|
+
end
|
15
|
+
|
16
|
+
def include?(value)
|
17
|
+
@scope.exists?(@attr => value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/slug_fu.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slug_fu/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slug_fu"
|
8
|
+
spec.version = SlugFu::VERSION
|
9
|
+
spec.authors = ["Bradley Schaefer"]
|
10
|
+
spec.email = ["bradley.schaefer@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Yet another gem for creating slugs from arbitrary strings}
|
13
|
+
spec.description = %q{What sets this slug gem apart is its pluggable naming strategy and that it validates slug uniqueness}
|
14
|
+
spec.homepage = "https://www.github.com/soulcutter/slug_fu"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slug_fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bradley Schaefer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-30 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: What sets this slug gem apart is its pluggable naming strategy and that
|
70
|
+
it validates slug uniqueness
|
71
|
+
email:
|
72
|
+
- bradley.schaefer@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/slug_fu.rb
|
87
|
+
- lib/slug_fu/default_naming_strategy.rb
|
88
|
+
- lib/slug_fu/model_context.rb
|
89
|
+
- lib/slug_fu/version.rb
|
90
|
+
- slug_fu.gemspec
|
91
|
+
homepage: https://www.github.com/soulcutter/slug_fu
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.4.6
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Yet another gem for creating slugs from arbitrary strings
|
115
|
+
test_files: []
|