agnostic_slugs 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +10 -0
- data/lib/agnostic_slugs/slug.rb +13 -0
- data/lib/agnostic_slugs/version.rb +1 -1
- data/spec/unit/slug_spec.rb +9 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1a58e6b7b684d390a15b4d976983226571bdc15
|
4
|
+
data.tar.gz: 86d16686360559a0e5bf9d4be750925ee7d48e9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78ffb9bce0bde0e5483894b0fffc5ea64003c5166d11e2f3351a3e3d0c757577924cdae97e88fd383e32644849647cfc5b4fc3ddba2dcc38ee5c1d5b61b66c15
|
7
|
+
data.tar.gz: 3257ed2b7a37e6952528efe15ab946fa8f2cce4418a5b07404ebaf79710ec2762c723eed72e66c611641c146165ade2f677033b1fbe941b91eb2b24f21f0ae61
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -23,6 +23,16 @@ slug.to_s # => "look-at-my-pretty-new-shoes"
|
|
23
23
|
slug.next.to_s # => "look-at-my-pretty-new-shoes-2"
|
24
24
|
```
|
25
25
|
|
26
|
+
Or step through slugs to find a unique slug using your own business logic:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
Slug.step('Hello world') do |slug|
|
30
|
+
my_repo.slug_unique?(slug)
|
31
|
+
end
|
32
|
+
# => "hello-world-7"
|
33
|
+
```
|
34
|
+
|
35
|
+
|
26
36
|
## Contributing
|
27
37
|
|
28
38
|
1. Fork it ( https://github.com/lasseebert/agnostic_slugs/fork )
|
data/lib/agnostic_slugs/slug.rb
CHANGED
@@ -2,6 +2,10 @@ module AgnosticSlugs
|
|
2
2
|
class Slug
|
3
3
|
attr_reader :original, :counter
|
4
4
|
|
5
|
+
def self.step(name, &block)
|
6
|
+
new(name).step(&block)
|
7
|
+
end
|
8
|
+
|
5
9
|
def initialize(original, counter = 1)
|
6
10
|
@original = original
|
7
11
|
@counter = counter
|
@@ -11,6 +15,15 @@ module AgnosticSlugs
|
|
11
15
|
self.class.new(original, counter + 1)
|
12
16
|
end
|
13
17
|
|
18
|
+
def step
|
19
|
+
instance = self
|
20
|
+
loop do
|
21
|
+
break if yield(instance.to_s)
|
22
|
+
instance = instance.next
|
23
|
+
end
|
24
|
+
instance.to_s
|
25
|
+
end
|
26
|
+
|
14
27
|
def to_s
|
15
28
|
@slug ||= generate
|
16
29
|
end
|
data/spec/unit/slug_spec.rb
CHANGED
@@ -52,5 +52,14 @@ module AgnosticSlugs
|
|
52
52
|
expect(slug.counter).to eq(1)
|
53
53
|
end
|
54
54
|
end
|
55
|
+
|
56
|
+
describe '.step' do
|
57
|
+
it 'returns the slug that was yielded true' do
|
58
|
+
result = Slug.step('Foo Bar') do |slug|
|
59
|
+
slug =~ /-7$/
|
60
|
+
end
|
61
|
+
expect(result).to eq('foo-bar-7')
|
62
|
+
end
|
63
|
+
end
|
55
64
|
end
|
56
65
|
end
|