finishing_moves 0.6.0 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d56bf8257e6c87e6b608472cb3aa7aaafe5aa921
4
- data.tar.gz: e0f8790fb33d1b2b613429a133cb77725a90d011
3
+ metadata.gz: 69a404faf013aed724182275a9161c727b342964
4
+ data.tar.gz: 5aa8ac458731e843686630cb0c17684f3057a03c
5
5
  SHA512:
6
- metadata.gz: f406d28dbe0a08825c828aa3c856a7e7fd3910fc20cbda106babb058e4fff488feec211d0dceb06044f7e72978203034219c8b13a64db583d7122536ddae8aae
7
- data.tar.gz: eb8715f761ddb6a4b7871591ab8e5118901736f1b6371305bfaccca7be01f972b892cf4610444c8097ca4e282b8bb9ba047a81fd073eb7df9efbc9c534bfd3e5
6
+ metadata.gz: e8efc6183a72dfa6d7608442151d9e47a933e986dae04f069a48783cedc3319868dc74bb664182b122813dc8415d917830723bea57cdbaca0935595d376e7995
7
+ data.tar.gz: 164284b79295232351b67a00abb58afb106050bbe9f5b8ef69fe737a017c828838d613230a66d8f6ded2230ff32623c6c32087c90a394d0406713af82e281d1d
data/README.md CHANGED
@@ -49,6 +49,7 @@ Tested against **`2.0.0` and above**. Probably works in `1.9.3`.
49
49
  - [`Object#is_an?`](https://github.com/forgecrafted/finishing_moves/wiki/Object#objectis_an)
50
50
  - [`String#dedupe`](https://github.com/forgecrafted/finishing_moves/wiki/String#stringdedupe)
51
51
  - [`String#keyify`](https://github.com/forgecrafted/finishing_moves/wiki/String#stringkeyify)
52
+ - [`String#slugify`](https://github.com/forgecrafted/finishing_moves/wiki/String#stringslugify) *<-- new in 0.6.0!*
52
53
  - [`String#match?`](https://github.com/forgecrafted/finishing_moves/wiki/String#stringmatch)
53
54
  - [`String#nl2br`](https://github.com/forgecrafted/finishing_moves/wiki/String#stringnl2br)
54
55
  - [`String#remove_whitespace`](https://github.com/forgecrafted/finishing_moves/wiki/String#stringremove_whitespace)
@@ -5,46 +5,23 @@ class String
5
5
  end
6
6
 
7
7
  def keyify
8
- result = nil_chain do
9
- the_key = self.clone
10
-
11
- # strip all non-alphanumerics
12
- the_key.gsub!(/[^0-9a-z]+/i, '_')
13
-
14
- # borrowing some logic from Rails method underscore
15
- # http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore
16
- if the_key =~ /[A-Z-]|::/
17
- the_key.gsub!(/::/, '_')
18
- the_key.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
19
- the_key.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
20
- end
21
-
22
- # Strip any leading numbers and underscores
23
- the_key.lstrip_all!('0-9_')
24
- # Strip trailing underscores
25
- the_key.rstrip_all!('_')
26
- # Combine multiple concurrent underscores
27
- the_key.dedupe!('_')
28
- # lowercase
29
- the_key.downcase!
30
- next the_key
31
- end
32
- return nil if result.nil? || result == ''
33
- result.to_sym
8
+ _prep_key_or_slug(true)
34
9
  end
35
10
 
36
11
  def keyify!
37
- result = self.keyify
12
+ result = _prep_key_or_slug(true)
38
13
  raise ArgumentError, "#{self.inspect} cannot be keyified, no valid characters" if result.nil?
39
14
  result
40
15
  end
41
16
 
42
17
  def slugify
43
- _slugify_gsub(self.keyify)
18
+ _prep_key_or_slug(false)
44
19
  end
45
20
 
46
21
  def slugify!
47
- _slugify_gsub(self.keyify!)
22
+ result = _prep_key_or_slug(false)
23
+ raise ArgumentError, "#{self.inspect} cannot be slugified, no valid characters" if result.nil?
24
+ result
48
25
  end
49
26
 
50
27
  def strip_all(chars = nil)
@@ -122,9 +99,40 @@ class String
122
99
  expr << ' '
123
100
  end
124
101
 
125
- def _slugify_gsub(str)
126
- return nil if str.nil?
127
- str.to_s.gsub('_', '-')
102
+ def _prep_key_or_slug(keyified = true)
103
+ result = nil_chain do
104
+ the_key = self.clone
105
+
106
+ # strip all non-alphanumerics
107
+ the_key.gsub!(/[^0-9a-z]+/i, '_')
108
+
109
+ # borrowing some logic from Rails method underscore
110
+ # http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore
111
+ if the_key =~ /[A-Z-]|::/
112
+ the_key.gsub!(/::/, '_')
113
+ the_key.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
114
+ the_key.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
115
+ end
116
+
117
+ # Strip any leading numbers (keyify only)
118
+ the_key.lstrip_all!('0-9') if keyified
119
+ # Strip any leading or trailing underscores
120
+ the_key.strip_all!('_')
121
+ # Combine multiple concurrent underscores
122
+ the_key.dedupe!('_')
123
+ # lowercase
124
+ the_key.downcase!
125
+ # replace underscore with dashes (slugify only)
126
+ the_key.gsub!('_', '-') unless keyified
127
+ next the_key
128
+ end
129
+ case
130
+ when result.nil? || result == ''
131
+ nil
132
+ when keyified
133
+ result.to_sym
134
+ else result
135
+ end
128
136
  end
129
137
 
130
138
  end
@@ -1,3 +1,3 @@
1
1
  module FinishingMoves
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
data/spec/string_spec.rb CHANGED
@@ -162,10 +162,8 @@ describe String do
162
162
  expect(:FooBarBaz.slugify).to eq 'foo-bar-baz'
163
163
  expect("Foo-Bar'Baz".slugify).to eq 'foo-bar-baz'
164
164
  expect('(Foo*&Bar!Baz?'.slugify).to eq 'foo-bar-baz'
165
- expect('1234FooBAR'.slugify).to eq 'foo-bar'
166
165
  expect('!@#$Foo0987'.slugify).to eq 'foo0987'
167
166
  expect('!@#$%^'.slugify).to eq nil
168
- expect('12345678'.slugify).to eq nil
169
167
  expect("Bill O'Shea".slugify).to eq 'bill-o-shea'
170
168
  expect("Bill O Shea".slugify).to eq 'bill-o-shea'
171
169
  expect("Bill O Shea".slugify).to eq 'bill-o-shea'
@@ -174,11 +172,16 @@ describe String do
174
172
  str = 'FooBarBaz'
175
173
  expect(str.slugify).to eq 'foo-bar-baz'
176
174
  expect(str).to eq 'FooBarBaz'
175
+
176
+ # we permit leading numbers, unlike keyify
177
+ expect('!1234FooBAR'.slugify).to eq '1234-foo-bar'
178
+ expect('1234FooBAR'.slugify).to eq '1234-foo-bar'
179
+ expect('12345678'.slugify).to eq '12345678'
177
180
  end
178
181
 
179
182
  it '#slugify!' do
180
183
  expect{ '!@#$%^'.slugify! }.to raise_error(ArgumentError)
181
- expect{ '12345678'.slugify! }.to raise_error(ArgumentError)
184
+ expect{ '12345678'.slugify! }.not_to raise_error
182
185
  end
183
186
 
184
187
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finishing_moves
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Koehl
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-10 00:00:00.000000000 Z
12
+ date: 2015-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rb-readline