symbolizify 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -7
- data/lib/symbolizify.rb +6 -2
- data/lib/symbolizify/version.rb +1 -1
- data/spec/symbolizify_spec.rb +30 -7
- data/symbolizify.gemspec +1 -1
- metadata +4 -3
data/README.md
CHANGED
@@ -2,19 +2,22 @@
|
|
2
2
|
|
3
3
|
Converts the given string to a symbol-style symbol.
|
4
4
|
|
5
|
+
Adds `ActiveSupport::Inflectors#symbolizify`, `String#symbolizify!`, and `String#symbolizify`.
|
6
|
+
|
5
7
|
This is intended to be a kind of inverse of `#humanize`, but a little more robust than [dehumanize](https://github.com/AndyObtiva/dehumanize). This is a strong symbolizer, converting hyphens, non-standard characters, spaces, camelcase, etc. to underscored strings. The main inspiration for this came after finding that `#dehumanize` did not support dehumanizing strings containing numbers, e.g.
|
6
8
|
|
7
9
|
```ruby
|
8
|
-
"Abstract Object 1".dehumanize
|
10
|
+
"Abstract Object 1".dehumanize # => "abstract_object1"
|
11
|
+
"Abstract Object 1".symbolizify # => "abstract_object_1"
|
9
12
|
```
|
10
13
|
|
11
|
-
|
14
|
+
The name `symbolizify` was chosen in order to avoid naming collisions or confusion with other `symbolize` gems in the market, and to reinforce the idea that this doesn't actually convert a string to a symbol, but rather converts it to a symbol-style string.
|
12
15
|
|
13
16
|
## Installation
|
14
17
|
|
15
18
|
Add this line to your application's Gemfile:
|
16
19
|
|
17
|
-
gem 'symbolizify', '>= 0.
|
20
|
+
gem 'symbolizify', '>= 0.2.0'
|
18
21
|
|
19
22
|
And then execute:
|
20
23
|
|
@@ -26,16 +29,17 @@ Or install it yourself as:
|
|
26
29
|
|
27
30
|
## Usage
|
28
31
|
|
29
|
-
Call symbolizify on any string to turn it into a symbol-style string! Removes non-word/non-digit characters
|
32
|
+
Call symbolizify on any string to turn it into a symbol-style string! Removes non-word/non-digit characters, reduces everything to lowercase with underscores separating words and numbers, e.g.
|
30
33
|
|
31
34
|
```ruby
|
32
|
-
'Personal Phone'.symbolizify
|
35
|
+
'Personal Phone '.symbolizify # => 'personal_phone',
|
33
36
|
'Home address'.symbolizify # => 'home_address',
|
34
37
|
'HatRack'.symbolizify # => 'hat_rack',
|
35
|
-
'Who is _why?'.symbolizify # => 'who_is_why
|
38
|
+
'Who is _why?'.symbolizify # => 'who_is_why',
|
36
39
|
'Person 1'.symbolizify # => 'person_1',
|
37
40
|
'Personel! #231'.symbolizify # => 'personel_231',
|
38
|
-
'
|
41
|
+
'Wang chung !'.symbolizify # => 'wang_chung',
|
42
|
+
'Shekibobo is great!'.symbolizify # => 'shekibobo_is_great',
|
39
43
|
'test.subject@example.com'.symbolizify # => 'test_subject_example_com'
|
40
44
|
```
|
41
45
|
|
data/lib/symbolizify.rb
CHANGED
@@ -3,12 +3,16 @@ require "active_support/inflector"
|
|
3
3
|
|
4
4
|
module ActiveSupport::Inflector
|
5
5
|
def symbolizify(s)
|
6
|
-
|
6
|
+
parameterize(titleize(s), '_')
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
class String
|
11
11
|
def symbolizify
|
12
|
-
|
12
|
+
dup.symbolizify!
|
13
|
+
end
|
14
|
+
|
15
|
+
def symbolizify!
|
16
|
+
self.replace ActiveSupport::Inflector.symbolizify(self)
|
13
17
|
end
|
14
18
|
end
|
data/lib/symbolizify/version.rb
CHANGED
data/spec/symbolizify_spec.rb
CHANGED
@@ -4,18 +4,41 @@ require 'symbolizify'
|
|
4
4
|
describe String do
|
5
5
|
describe "#symbolizify" do
|
6
6
|
{
|
7
|
-
'Personal Phone' => 'personal_phone',
|
8
|
-
'Home address'
|
9
|
-
'HatRack'
|
10
|
-
'Who is _why?'
|
11
|
-
'Person 1'
|
12
|
-
'Personel! #231'
|
13
|
-
'
|
7
|
+
'Personal Phone ' => 'personal_phone',
|
8
|
+
'Home address' => 'home_address',
|
9
|
+
'HatRack' => 'hat_rack',
|
10
|
+
'Who is _why?' => 'who_is_why',
|
11
|
+
'Person 1' => 'person_1',
|
12
|
+
'Personel! #231' => 'personel_231',
|
13
|
+
'Wang chung !' => 'wang_chung',
|
14
|
+
'Shekibobo is great!' => 'shekibobo_is_great',
|
14
15
|
'test.subject@example.com' => 'test_subject_example_com'
|
15
16
|
}.each do |original, symbolizified|
|
16
17
|
it "should turn '#{original}' to '#{symbolizified}'" do
|
17
18
|
original.symbolizify.should eq symbolizified
|
18
19
|
end
|
19
20
|
end
|
21
|
+
|
22
|
+
it "should return a copy of the original string" do
|
23
|
+
original = original_copy = "I don't know what I was expecting"
|
24
|
+
original.symbolizify
|
25
|
+
original.should eq original_copy
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#symbolizify!" do
|
30
|
+
it "should modify the string in place" do
|
31
|
+
string = "Gangnam Style"
|
32
|
+
string.symbolizify!
|
33
|
+
string.should eq "gangnam_style"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe ActiveSupport::Inflector do
|
39
|
+
describe "#symbolizify" do
|
40
|
+
it "should coerce non-strings to strings prior to conversion" do
|
41
|
+
ActiveSupport::Inflector.symbolizify(%w[array of strings!]).should eq "array_of_strings"
|
42
|
+
end
|
20
43
|
end
|
21
44
|
end
|
data/symbolizify.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = Symbolizify::VERSION
|
9
9
|
gem.authors = ["Joshua Kovach"]
|
10
10
|
gem.email = ["kovach.jc@gmail.com"]
|
11
|
-
gem.description = %q{Converts any string into a symbol-style
|
11
|
+
gem.description = %q{Converts any string into a symbol-style string.}
|
12
12
|
gem.summary = %q{Adds a method `#symbolizify` on ActiveSupport::Inflector and String which converts a string into a symbol-style string.}
|
13
13
|
gem.homepage = "https://www.github.com/shekibobo/symbolizify"
|
14
14
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: symbolizify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '2.5'
|
46
|
-
description: Converts any string into a symbol-style
|
46
|
+
description: Converts any string into a symbol-style string.
|
47
47
|
email:
|
48
48
|
- kovach.jc@gmail.com
|
49
49
|
executables: []
|
@@ -88,3 +88,4 @@ summary: Adds a method `#symbolizify` on ActiveSupport::Inflector and String whi
|
|
88
88
|
test_files:
|
89
89
|
- spec/spec_helper.rb
|
90
90
|
- spec/symbolizify_spec.rb
|
91
|
+
has_rdoc:
|