simple_symbolize 3.0.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -1
- data/README.md +33 -5
- data/lib/simple_symbolize/core_ext/string/symbolize.rb +36 -0
- data/lib/simple_symbolize/version.rb +1 -1
- data/lib/simple_symbolize.rb +39 -16
- data/simple_symbolize.gemspec +2 -4
- metadata +18 -3
- data/lib/simple_symbolize/string.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69d7f99fc0ead5eeda918161fe2995fb4b58bcd39e297dae0af8d7efceb0b030
|
4
|
+
data.tar.gz: e5685b751c32abb542e6599f9677a785d7584be97d0c47a45067125192e0687e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 196774c36c21335f2699a7db17dbe10e7df3727533b9d53b401c852371d8347aaf900dbea9811e948b23d333b344d44b11bfff4dbce99888e0ac70557f67304d
|
7
|
+
data.tar.gz: c7e76fa9f182ad23ccef97ab8e47a40626316369dd2bf187609a292043c0f91a432455399d086b79eb61daf0aa2ba46accc23bab72946a7dd3b47de5d922400e
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -17,7 +17,7 @@ It works by removing special characters in a String like `'!'` and underscoring
|
|
17
17
|
'hello world!'.to_sym # => :"hello world!"
|
18
18
|
|
19
19
|
# Symbolize gem
|
20
|
-
'hello world!'.
|
20
|
+
'hello world!'.simple_symbolize # => :hello_world
|
21
21
|
```
|
22
22
|
|
23
23
|
## Installation
|
@@ -53,7 +53,9 @@ SimpleSymbolize.symbolize('hello world!') # => :hello_world
|
|
53
53
|
```ruby
|
54
54
|
require 'simple_symbolize'
|
55
55
|
|
56
|
-
|
56
|
+
String.include SimpleSymbolize::CoreExt::String
|
57
|
+
|
58
|
+
'hello world!'.simple_symbolize # => :hello_world
|
57
59
|
```
|
58
60
|
|
59
61
|
## Configuration
|
@@ -72,15 +74,41 @@ end
|
|
72
74
|
|
73
75
|
## Updates!
|
74
76
|
|
77
|
+
### V4
|
78
|
+
#### String methods now need to be Mixed in
|
79
|
+
|
80
|
+
SimpleSymbolize is safe to use with other gems, particularly the popular ActiveSupport gem which SimpleSymbolize use to share
|
81
|
+
certain methods names with.
|
82
|
+
|
83
|
+
You now need to deliberatly mixin the methods on the String class:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
String.include SimpleSymbolize::CoreExt::String
|
87
|
+
```
|
88
|
+
|
89
|
+
To make them easier to spot, the method names on the String class have been prefixed with `simple_` to avoid confusion.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
'Hello World!'.simple_symbolize #=> :hello_world
|
93
|
+
'Hello World!'.simple_elementize #=> 'hello_world'
|
94
|
+
'Hello World!'.simple_camelize #=> :helloWorld
|
95
|
+
'Hello World!'.simple_snakeize #=> :hello_world
|
96
|
+
```
|
97
|
+
|
98
|
+
#### Introducing #snakeize
|
99
|
+
|
100
|
+
The `#snakeize` method will return your object in snake_case.
|
101
|
+
This is the default behaviour of the `#symbolize` method however `#snakeize` will always return thr Symbol in snake_case.
|
102
|
+
|
75
103
|
### V3
|
76
|
-
#### String to_snake_case
|
104
|
+
#### String to_snake_case [DEPRECATED - replaced with `#simple_snakeize` in v4]
|
77
105
|
|
78
106
|
`#to_snake_case` extends the String class to return you your String object in snake_case format.
|
79
107
|
|
80
108
|
#### Handle camelCase with Symbolize
|
81
109
|
|
82
110
|
```ruby
|
83
|
-
symbolize('helloWorld!') # => :hello_world
|
111
|
+
SimpleSymbolize.symbolize('helloWorld!') # => :hello_world
|
84
112
|
```
|
85
113
|
|
86
114
|
This is the default behaviour and can be switched off by setting `#handle_camel_case` to `false`
|
@@ -109,7 +137,7 @@ Sometimes you just want a simple String obj without all the fuss. Elementize tak
|
|
109
137
|
and returns you a simple-to-use String.
|
110
138
|
|
111
139
|
```ruby
|
112
|
-
elementize('hello world!') # => "hello_world"
|
140
|
+
SimpleSymbolize.elementize('hello world!') # => "hello_world"
|
113
141
|
```
|
114
142
|
|
115
143
|
#### Camelize
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Extends the String class by mixing in the symbolize module.
|
4
|
+
# @example Mixin the methods to the String class
|
5
|
+
# String.include SimpleSymbolize::CoreExt::String
|
6
|
+
|
7
|
+
module SimpleSymbolize
|
8
|
+
module CoreExt
|
9
|
+
# Contains methods to be mixed into the String class
|
10
|
+
module String
|
11
|
+
# @example Symbolize a string using the String object method
|
12
|
+
# "hello world!".symbolize #=> :hello_world
|
13
|
+
def simple_symbolize
|
14
|
+
SimpleSymbolize.symbolize(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
# @example Turns a String into a camelCase Symbol
|
18
|
+
# "Hello World".simple_camelize => :helloWorld
|
19
|
+
def simple_camelize
|
20
|
+
SimpleSymbolize.camelize(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
# @example Symbolizes a String then calls #to_s
|
24
|
+
# "helloWorld".simple_elementize => 'hello_word'
|
25
|
+
def simple_elementize
|
26
|
+
SimpleSymbolize.elementize(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @example Turns a String into it's snake_case equivalent
|
30
|
+
# "helloWorld".simple_snakeize => 'hello_word'
|
31
|
+
def simple_snakeize
|
32
|
+
SimpleSymbolize.snakeize(self)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/simple_symbolize.rb
CHANGED
@@ -2,10 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'simple_symbolize/version'
|
4
4
|
|
5
|
-
require_relative 'simple_symbolize/string'
|
6
5
|
require_relative 'simple_symbolize/translations'
|
7
|
-
|
8
|
-
include SimpleSymbolize
|
6
|
+
require_relative 'simple_symbolize/core_ext/string/symbolize'
|
9
7
|
|
10
8
|
# Main module for the gem
|
11
9
|
# Contains the base methods and allows configuration
|
@@ -31,18 +29,21 @@ module SimpleSymbolize
|
|
31
29
|
# @param obj [Object] the String object to be symbolized.
|
32
30
|
#
|
33
31
|
# @example Symbolize a string using the symbolize method
|
34
|
-
# symbolize("hello world!") #=> :hello_world
|
35
|
-
def symbolize(obj)
|
32
|
+
# SimpleSymbolize.symbolize("hello world!") #=> :hello_world
|
33
|
+
def self.symbolize(obj)
|
36
34
|
return obj unless obj.respond_to?(:to_s)
|
37
35
|
return obj if [Hash, Array, NilClass].include?(obj.class)
|
36
|
+
return obj if obj.respond_to?(:empty?) && obj.empty?
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
obj = if SimpleSymbolize.translations.handle_camel_case
|
39
|
+
snakeize(obj)
|
40
|
+
else
|
41
|
+
obj.to_s
|
42
|
+
.downcase
|
43
|
+
.gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
|
44
|
+
.gsub(Regexp.union(SimpleSymbolize.translations.remove), '')
|
45
|
+
end
|
46
|
+
obj.to_sym
|
46
47
|
end
|
47
48
|
|
48
49
|
# Symbolizes a String object and returns it as a String object.
|
@@ -50,10 +51,11 @@ module SimpleSymbolize
|
|
50
51
|
# @param obj [Object] the object to be symbolized.
|
51
52
|
#
|
52
53
|
# @example Elementize a string using the elementize method
|
53
|
-
# elementize("hello world!") #=> "helloWorld"
|
54
|
-
def elementize(obj)
|
54
|
+
# SimpleSymbolize.elementize("hello world!") #=> "helloWorld"
|
55
|
+
def self.elementize(obj)
|
55
56
|
return obj unless obj.respond_to?(:to_s)
|
56
57
|
return obj if [Hash, Array, NilClass].include?(obj.class)
|
58
|
+
return obj if obj.respond_to?(:empty?) && obj.empty?
|
57
59
|
|
58
60
|
symbolize(obj).to_s
|
59
61
|
end
|
@@ -63,14 +65,35 @@ module SimpleSymbolize
|
|
63
65
|
# @param obj [Object] the String object to be camelized.
|
64
66
|
#
|
65
67
|
# @example Camelize a string using the camelize method
|
66
|
-
# camelize("hello world!") #=> :helloWorld
|
67
|
-
def camelize(obj)
|
68
|
+
# SimpleSymbolize.camelize("hello world!") #=> :helloWorld
|
69
|
+
def self.camelize(obj)
|
68
70
|
return obj unless obj.respond_to?(:to_s)
|
69
71
|
return obj if [Hash, Array, NilClass].include?(obj.class)
|
72
|
+
return obj if obj.respond_to?(:empty?) && obj.empty?
|
70
73
|
|
71
74
|
first, *rest = elementize(obj).split('_')
|
72
75
|
return obj if first.nil?
|
73
76
|
|
74
77
|
rest.size.positive? ? (first << rest.map(&:capitalize).join).to_sym : symbolize(first)
|
75
78
|
end
|
79
|
+
|
80
|
+
# Turns a String || Symbol into a snake_case Symbol
|
81
|
+
#
|
82
|
+
# @param obj [Object] the object to be snakeize
|
83
|
+
#
|
84
|
+
# @example Snakeize an object using the snakeize method
|
85
|
+
# SimpleSymbolize.snakeize('Hello World!') #=> :hello_world
|
86
|
+
def self.snakeize(obj)
|
87
|
+
return obj unless obj.respond_to?(:to_s)
|
88
|
+
return obj if [Hash, Array, NilClass].include?(obj.class)
|
89
|
+
return obj if obj.respond_to?(:empty?) && obj.empty?
|
90
|
+
|
91
|
+
obj.to_s
|
92
|
+
.gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
|
93
|
+
.gsub(Regexp.union(SimpleSymbolize.translations.remove), '')
|
94
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
95
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
96
|
+
.downcase
|
97
|
+
.to_sym
|
98
|
+
end
|
76
99
|
end
|
data/simple_symbolize.gemspec
CHANGED
@@ -5,7 +5,7 @@ require_relative 'lib/simple_symbolize/version'
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'simple_symbolize'
|
7
7
|
spec.version = SimpleSymbolize::VERSION
|
8
|
-
spec.authors = ['alexo']
|
8
|
+
spec.authors = ['alexo', 'Driver and Vehicle Licensing Agency (DVLA)']
|
9
9
|
spec.email = ['']
|
10
10
|
|
11
11
|
spec.summary = 'Turns Strings into Symbols.'
|
@@ -18,11 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.license = 'MIT'
|
19
19
|
spec.required_ruby_version = Gem::Requirement.new('>= 3.0')
|
20
20
|
|
21
|
-
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
22
|
-
|
23
21
|
spec.metadata['homepage_uri'] = spec.homepage
|
24
22
|
spec.metadata['source_code_uri'] = spec.homepage
|
25
|
-
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
26
23
|
|
27
24
|
# Specify which files should be added to the gem when it is released.
|
28
25
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -37,4 +34,5 @@ Gem::Specification.new do |spec|
|
|
37
34
|
spec.add_development_dependency 'rake', '~> 13.0'
|
38
35
|
spec.add_development_dependency 'rspec', '~> 3.12'
|
39
36
|
spec.add_development_dependency 'rubocop', '~> 1.54'
|
37
|
+
spec.add_development_dependency 'simplecov', '~> 0.22'
|
40
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_symbolize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alexo
|
8
|
+
- Driver and Vehicle Licensing Agency (DVLA)
|
8
9
|
autorequire:
|
9
10
|
bindir: exe
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2024-01-12 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: pry
|
@@ -66,6 +67,20 @@ dependencies:
|
|
66
67
|
- - "~>"
|
67
68
|
- !ruby/object:Gem::Version
|
68
69
|
version: '1.54'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: simplecov
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0.22'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0.22'
|
69
84
|
description: 'simple_symbolize will remove special characters from a String, replacing
|
70
85
|
whitespace with an underscore, down-casing and finally calling the #to_sym String
|
71
86
|
method. Configure this gem to your hearts content!'
|
@@ -88,7 +103,7 @@ files:
|
|
88
103
|
- bin/console
|
89
104
|
- bin/setup
|
90
105
|
- lib/simple_symbolize.rb
|
91
|
-
- lib/simple_symbolize/string.rb
|
106
|
+
- lib/simple_symbolize/core_ext/string/symbolize.rb
|
92
107
|
- lib/simple_symbolize/translations.rb
|
93
108
|
- lib/simple_symbolize/version.rb
|
94
109
|
- simple_symbolize.gemspec
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'simple_symbolize'
|
4
|
-
|
5
|
-
# Extends the String class by mixing in the symbolize module.
|
6
|
-
class String
|
7
|
-
# @example Symbolize a string using the String object method
|
8
|
-
# "hello world!".symbolize #=> :hello_world
|
9
|
-
def symbolize
|
10
|
-
SimpleSymbolize.symbolize(self)
|
11
|
-
end
|
12
|
-
|
13
|
-
# @example Turns a String into it's snake_case equivalent
|
14
|
-
# "helloWorld".to_snake_case => 'hello_word'
|
15
|
-
def to_snake_case
|
16
|
-
# rubocop:disable Style/RedundantSelf
|
17
|
-
self.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
18
|
-
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
19
|
-
.downcase
|
20
|
-
# rubocop:enable Style/RedundantSelf
|
21
|
-
end
|
22
|
-
end
|