simple_symbolize 4.1.0 → 6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5fe69f67b8c3e4152dd088e6425f0a0e9c0c5206ff384bd599284f976b664e4e
4
- data.tar.gz: d8ce3414da304b386a71e83f482bd3dc2a031274c61dbe8d69efbab1d9dc819a
3
+ metadata.gz: a61b74fb95d9b3e3e76a3256bc88cb1f7b35c684ba6c1ea18fb1eeaee09ace87
4
+ data.tar.gz: e4a37ab07e24568f3434d8725e3fcc63d20b5d2d06e429842bcbfd4109709d7f
5
5
  SHA512:
6
- metadata.gz: da85cabac7e7f368cfd7a0366dffa7642e6f0b27f2cbac49b548264df55f82eb715d74cf80e2113a172b7451b2496a86ddcbc13ef818d66e8b7f72aa8df80ff1
7
- data.tar.gz: 82a0bd3a48cecbf2ca49607dd136cc8dc46a3babb761507b88a74043a494f396fbe6e7ecc53393c5a2d065e2f8d0302ccfdf80dfe6cb2412d3188091f15d2f44
6
+ metadata.gz: 572ffe34944c55a5f805b05ede3af67c41789f4f2218e4d0a79a242b9d3599dc60a7e9e656590f18ba3fc2215fc4511d33445f3309d3e482cf52c89d8768010e
7
+ data.tar.gz: 48cdddc6da798147b3104bb94f6fa88f2d7752120d7eec66aaca43edfc4c46fcc4728117ec81944c3d592cab210e63f179037b8c3a913a98bad3d2f5b54b18a4
@@ -18,10 +18,10 @@ jobs:
18
18
  runs-on: ubuntu-latest
19
19
  strategy:
20
20
  matrix:
21
- ruby-version: [ '3.2', '3.3', '3.4' ]
21
+ ruby-version: [ '3.4', '4.0' ]
22
22
 
23
23
  steps:
24
- - uses: actions/checkout@v2
24
+ - uses: actions/checkout@v4
25
25
  - name: Set up Ruby
26
26
  uses: ruby/setup-ruby@v1
27
27
  with:
@@ -9,10 +9,10 @@ jobs:
9
9
  runs-on: ubuntu-latest
10
10
  strategy:
11
11
  matrix:
12
- ruby-version: [ '3.2', '3.3', '3.4' ]
12
+ ruby-version: [ '3.4', '4.0' ]
13
13
 
14
14
  steps:
15
- - uses: actions/checkout@v2
15
+ - uses: actions/checkout@v4
16
16
  - name: Set up Ruby
17
17
  uses: ruby/setup-ruby@v1
18
18
  with:
@@ -31,11 +31,11 @@ jobs:
31
31
  contents: read
32
32
 
33
33
  steps:
34
- - uses: actions/checkout@v2
35
- - name: Set up Ruby 3.0.1
34
+ - uses: actions/checkout@v4
35
+ - name: Set up Ruby 4.0
36
36
  uses: ruby/setup-ruby@v1
37
37
  with:
38
- ruby-version: 3.0.1
38
+ ruby-version: 4.0
39
39
  bundler-cache: true
40
40
 
41
41
  - name: Publish to RubyGems
data/.rubocop.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  AllCops:
2
2
  NewCops: enable
3
3
  SuggestExtensions: false
4
- TargetRubyVersion: 3.0
4
+ TargetRubyVersion: 3.4
5
5
 
6
6
  Gemspec/DevelopmentDependencies:
7
7
  EnforcedStyle: gemspec
@@ -17,17 +17,8 @@ Metrics/BlockLength:
17
17
  Style/MixinUsage:
18
18
  Enabled: false
19
19
 
20
- Lint/BooleanSymbol:
21
- Enabled: false
22
-
23
20
  Metrics/AbcSize:
24
- Enabled: false
21
+ Max: 20
25
22
 
26
23
  Metrics/MethodLength:
27
- Max: 25
28
-
29
- Layout/LineLength:
30
- Max: 130
31
-
32
- Metrics/CyclomaticComplexity:
33
- Max: 8
24
+ Max: 20
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.4.5
1
+ ruby-4.0.5
data/Gemfile CHANGED
@@ -5,7 +5,9 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in simple_symbolize.gemspec
6
6
  gemspec
7
7
 
8
- gem 'rake', '~> 13.3'
8
+ gem 'rake', '~> 13.4'
9
9
  gem 'rspec', '~> 3.13'
10
- gem 'rubocop', '~> 1.79'
10
+ gem 'rubocop', '~> 1.88'
11
11
  gem 'simplecov', '~> 0.22'
12
+
13
+ gem 'irb', '~> 1.18'
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # SimpleSymbolize
2
2
 
3
- SimpleSymbolize takes a string and transforms it into a symbol. Why? Because working with symbols in Ruby makes for a
3
+ SimpleSymbolize takes a string and transforms it into a symbol. Why? Because working with symbols in Ruby makes for a
4
4
  good time.
5
5
 
6
6
  Wait, doesn't String already have a `to_sym` method?
7
7
 
8
- Correct! However, this gem takes it one step further by transforming special characters and whitespace to give you a
8
+ Correct! However, this gem takes it one step further by transforming special characters and whitespace to give you a
9
9
  simple easy to work with Symbol.
10
10
 
11
11
  It works by removing special characters in a String like `'!'` and underscoring any whitespace.
@@ -38,9 +38,9 @@ Or install it yourself as:
38
38
 
39
39
  ## Usage
40
40
 
41
- There are two ways to symbolize your String.
41
+ There are three ways to use this gem.
42
42
 
43
- ### Call the symbolize method and pass it your String
43
+ ### SimpleSymbolize
44
44
 
45
45
  ```ruby
46
46
  require 'simple_symbolize'
@@ -48,7 +48,16 @@ require 'simple_symbolize'
48
48
  SimpleSymbolize.symbolize('hello world!') # => :hello_world
49
49
  ```
50
50
 
51
- ### Call the symbolize method on your String object
51
+ Available methods:
52
+
53
+ | Method | What does it do? |
54
+ |------------|------------------------------------------------------------------------------------------------------------------------------------------|
55
+ | symbolize | Returns your object as a plain old symbol |
56
+ | elementize | Returns your object as a plain old String |
57
+ | camelize | Returns your object as a plain old camelCase symbol |
58
+ | snakeize | Returns your object as a plain old snake_case symbol (very similar to symbolize, only it does not respect the handle_camel_case setting) | |
59
+
60
+ ### Mixing into String
52
61
 
53
62
  ```ruby
54
63
  require 'simple_symbolize'
@@ -58,7 +67,16 @@ String.include SimpleSymbolize::CoreExt::String
58
67
  'hello world!'.simple_symbolize # => :hello_world
59
68
  ```
60
69
 
61
- ### Call the symbolize method on your Symbol object
70
+ Available methods:
71
+
72
+ | Method | What does it do? |
73
+ |------------|------------------------------------------------------------------------------------------------------------------------------------------|
74
+ | simple_symbolize | Returns your object as a plain old symbol |
75
+ | simple_elementize | Returns your object as a plain old String |
76
+ | simple_camelize | Returns your object as a plain old camelCase symbol |
77
+ | simple_snakeize | Returns your object as a plain old snake_case symbol (very similar to symbolize, only it does not respect the handle_camel_case setting) | |
78
+
79
+ ### Mixing into Symbol
62
80
 
63
81
  ```ruby
64
82
  require 'simple_symbolize'
@@ -68,9 +86,18 @@ Symbol.include SimpleSymbolize::CoreExt::Symbol
68
86
  :hello_world!.simple_symbolize # => :hello_world
69
87
  ```
70
88
 
89
+ Available methods:
90
+
91
+ | Method | What does it do? |
92
+ |------------|------------------------------------------------------------------------------------------------------------------------------------------|
93
+ | simple_symbolize | Returns your object as a plain old symbol |
94
+ | simple_elementize | Returns your object as a plain old String |
95
+ | simple_camelize | Returns your object as a plain old camelCase symbol |
96
+ | simple_snakeize | Returns your object as a plain old snake_case symbol (very similar to symbolize, only it does not respect the handle_camel_case setting) | |
97
+
71
98
  ## Configuration
72
99
 
73
- Something not underscored or removed? Or even something underscored/removed that you didn't want transformed?
100
+ Something not underscored or removed? Or even something underscored/removed that you didn't want transformed?
74
101
 
75
102
  No sweat, you can configure this gem to underscore and remove to your hearts content!
76
103
 
@@ -84,20 +111,67 @@ end
84
111
 
85
112
  ## Updates!
86
113
 
114
+ ### V6.0.0
115
+
116
+ #### Dropped support for Ruby < 3.4
117
+
118
+ Ruby 3.2 reached EOL in 2026, and Ruby 3.3 will reach EOL in 2027.
119
+ As such this gem no longer supports versions older than 3.4.
120
+
121
+ #### Inline configuration
122
+
123
+ You can now prevent chars being removed inline:
124
+
125
+ ```ruby
126
+ SimpleSymbolize.symbolize('Hello World!', strip_chars: false)
127
+ # => :hello_world!
128
+ ```
129
+
130
+ ### V5
131
+
132
+ #### Dropped support for Ruby < 3.2
133
+
134
+ Ruby 3.1 reached EOL in 2024, as such this gem no longer supports versions older than 3.2.
135
+
136
+ #### Only deals with Strings and Symbols
137
+
138
+ In previous iterations, certain non String/Symbol objects would be handled `SimpleSymbolize.symbolze(true)` returned
139
+ `:true`.
140
+
141
+ To be consistent, SimpleSymbolize now only accepts String/Symbols, everything else will be returned.
142
+
143
+ #### Uppercase acronyms when camelizing
144
+
145
+ Sometimes you want certain phrases to be in uppercase when camelizing, SimpleSymbolize now supports this.
146
+
147
+ ```ruby
148
+ SimpleSymbolize.translate { |t| t.camel_case_acronym = ['gb'] }
149
+
150
+ SimpleSymbolize.camelize('from gb') # => :fromGB (without setting the camel_case_acronym this would return :fromGb)
151
+ ```
152
+
153
+ ---
154
+
87
155
  ### V4.1
88
- ### Symbol methods can now be Mixed in
89
156
 
90
- SimpleSymbolize now supports mixing in the methods on the Symbol class, allowing you to call `simple_symbolize` directly on a Symbol object.
157
+ #### Symbol methods can now be Mixed in
158
+
159
+ SimpleSymbolize now supports mixing in the methods on the Symbol class, allowing you to call `simple_symbolize` directly
160
+ on a Symbol object.
91
161
 
92
162
  ```ruby
93
163
  Symbol.include SimpleSymbolize::CoreExt::Symbol
94
164
  :hello_world!.simple_symbolize # => :hello_world
95
165
  ```
96
166
 
167
+ ---
168
+
97
169
  ### V4
170
+
98
171
  #### String methods now need to be Mixed in
99
172
 
100
- SimpleSymbolize is safe to use with other gems, particularly the popular ActiveSupport gem which SimpleSymbolize use to share
173
+ SimpleSymbolize is safe to use with other gems, particularly the popular ActiveSupport gem which SimpleSymbolize use to
174
+ share
101
175
  certain methods names with.
102
176
 
103
177
  You now need to deliberately mixin the methods on the String class:
@@ -118,9 +192,13 @@ To make them easier to spot, the method names on the String class have been pref
118
192
  #### Introducing #snakeize
119
193
 
120
194
  The `#snakeize` method will return your object in snake_case.
121
- This is the default behaviour of the `#symbolize` method however `#snakeize` will always return thr Symbol in snake_case.
195
+ This is the default behaviour of the `#symbolize` method however `#snakeize` will always return thr Symbol in
196
+ snake_case.
197
+
198
+ ---
122
199
 
123
200
  ### V3
201
+
124
202
  #### String to_snake_case [DEPRECATED - replaced with `#simple_snakeize` in v4]
125
203
 
126
204
  `#to_snake_case` extends the String class to return you your String object in snake_case format.
@@ -145,6 +223,8 @@ Arrays are now supported when configuring the gem
145
223
  SimpleSymbolize.translate { |trans| trans.to_underscore = %w[!&*] }
146
224
  ```
147
225
 
226
+ ---
227
+
148
228
  ### V2
149
229
 
150
230
  SimpleSymbolize has got new friends!
@@ -162,7 +242,7 @@ SimpleSymbolize.elementize('hello world!') # => "hello_world"
162
242
 
163
243
  #### Camelize
164
244
 
165
- Great for working with APIs that require fields in a JSON format. Camelize clears away the clutter and returns you
245
+ Great for working with APIs that require fields in a JSON format. Camelize clears away the clutter and returns you
166
246
  a Symbolized object in camelCase.
167
247
 
168
248
  [comment]: <> (## Contributing)
data/bin/console CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  require 'bundler/setup'
5
5
  require 'simple_symbolize'
6
- require 'pry'
7
6
 
8
7
  # You can add fixtures and/or initialization code here to make experimenting
9
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,26 +10,26 @@ module SimpleSymbolize
10
10
  module String
11
11
  # @example Symbolize a string using the String object method
12
12
  # "hello world!".symbolize #=> :hello_world
13
- def simple_symbolize
14
- SimpleSymbolize.symbolize(self)
13
+ def simple_symbolize(strip_chars: true)
14
+ SimpleSymbolize.symbolize(self, strip_chars:)
15
15
  end
16
16
 
17
17
  # @example Turns a String into a camelCase Symbol
18
18
  # "Hello World".simple_camelize => :helloWorld
19
- def simple_camelize
20
- SimpleSymbolize.camelize(self)
19
+ def simple_camelize(strip_chars: true)
20
+ SimpleSymbolize.camelize(self, strip_chars:)
21
21
  end
22
22
 
23
23
  # @example Symbolizes a String then calls #to_s
24
24
  # "helloWorld".simple_elementize => 'hello_word'
25
- def simple_elementize
26
- SimpleSymbolize.elementize(self)
25
+ def simple_elementize(strip_chars: true)
26
+ SimpleSymbolize.elementize(self, strip_chars:)
27
27
  end
28
28
 
29
29
  # @example Turns a String into it's snake_case equivalent
30
30
  # "helloWorld".simple_snakeize => 'hello_word'
31
- def simple_snakeize
32
- SimpleSymbolize.snakeize(self)
31
+ def simple_snakeize(strip_chars: true)
32
+ SimpleSymbolize.snakeize(self, strip_chars:)
33
33
  end
34
34
  end
35
35
  end
@@ -10,26 +10,26 @@ module SimpleSymbolize
10
10
  module Symbol
11
11
  # @example Symbolize a string using the String object method
12
12
  # :hello_world!.symbolize #=> :hello_world
13
- def simple_symbolize
14
- SimpleSymbolize.symbolize(self)
13
+ def simple_symbolize(strip_chars: true)
14
+ SimpleSymbolize.symbolize(self, strip_chars:)
15
15
  end
16
16
 
17
17
  # @example Turns a String into a camelCase Symbol
18
18
  # :hello_world.simple_camelize => :helloWorld
19
- def simple_camelize
20
- SimpleSymbolize.camelize(self)
19
+ def simple_camelize(strip_chars: true)
20
+ SimpleSymbolize.camelize(self, strip_chars:)
21
21
  end
22
22
 
23
23
  # @example Symbolizes a String then calls #to_s
24
24
  # :helloWorld!.simple_elementize => 'hello_word'
25
- def simple_elementize
26
- SimpleSymbolize.elementize(self)
25
+ def simple_elementize(strip_chars: true)
26
+ SimpleSymbolize.elementize(self, strip_chars:)
27
27
  end
28
28
 
29
29
  # @example Turns a String into it's snake_case equivalent
30
30
  # :helloWorld.simple_snakeize => :hello_word
31
- def simple_snakeize
32
- SimpleSymbolize.snakeize(self)
31
+ def simple_snakeize(strip_chars: true)
32
+ SimpleSymbolize.snakeize(self, strip_chars:)
33
33
  end
34
34
  end
35
35
  end
@@ -10,8 +10,10 @@ module SimpleSymbolize
10
10
  attr_reader :remove
11
11
  # @return [Array] the characters to be untouched from the String.
12
12
  attr_reader :omit
13
-
13
+ # @return [Boolean] whether to handle camelCase strings during symbolisation.
14
14
  attr_reader :handle_camel_case
15
+ # @return [Array] the characters to be transformed into uppercase during camelisation.
16
+ attr_reader :camel_case_acronyms
15
17
 
16
18
  # Creates an instance of the Translations class.
17
19
  #
@@ -21,44 +23,44 @@ module SimpleSymbolize
21
23
  end
22
24
 
23
25
  # Merges the String passed with the @underscore Array omitting duplicates.
24
- # Removes those characters from the @remove and @omit Arrays to avoid the change being over-written.
26
+ # Removes those characters from @remove and @omit Arrays to avoid the change being over-written.
25
27
  #
26
- # @param chars [Array] an object containing characters to be underscored.
28
+ # @param input [Array] an object containing characters to be underscored.
27
29
  #
28
30
  # @return [Array] the Array of characters to be underscored.
29
- def to_underscore=(chars)
30
- chars = sanitise_chars(chars)
31
+ def to_underscore=(input)
32
+ arr = sanitise(input)
31
33
 
32
- @remove -= chars
33
- @omit -= chars
34
- @underscore |= chars
34
+ @remove -= arr
35
+ @omit -= arr
36
+ @underscore |= arr
35
37
  end
36
38
 
37
39
  # Merges the String passed with the @remove Array omitting duplicates.
38
- # Removes those characters from the @underscore and @omit Arrays to avoid the change being over-written.
40
+ # Removes those characters from @underscore and @omit Arrays to avoid the change being over-written.
39
41
  #
40
- # @param chars [String] a String object containing characters to be removed.
42
+ # @param input [String] a String object containing characters to be removed.
41
43
  #
42
44
  # @return [Array] the Array of characters to be removed.
43
- def to_remove=(chars)
44
- chars = sanitise_chars(chars)
45
+ def to_remove=(input)
46
+ arr = sanitise(input)
45
47
 
46
- @underscore -= chars
47
- @omit -= chars
48
- @remove |= chars
48
+ @underscore -= arr
49
+ @omit -= arr
50
+ @remove |= arr
49
51
  end
50
52
 
51
- # Removes characters within the String passed from the @remove and @underscore Arrays.
53
+ # Removes characters within the String passed from @remove and @underscore Arrays.
52
54
  #
53
- # @param chars [String] a String object containing characters to be removed.
55
+ # @param input [String] a String object containing characters to be removed.
54
56
  #
55
57
  # @return [Array] the Array of characters to be omitted.
56
- def to_omit=(chars)
57
- chars = sanitise_chars(chars)
58
+ def to_omit=(input)
59
+ arr = sanitise(input)
58
60
 
59
- @underscore -= chars
60
- @remove -= chars
61
- @omit += chars
61
+ @underscore -= arr
62
+ @remove -= arr
63
+ @omit += arr
62
64
  end
63
65
 
64
66
  def handle_camel_case=(handle)
@@ -68,26 +70,37 @@ module SimpleSymbolize
68
70
  @handle_camel_case = handle.eql?('true')
69
71
  end
70
72
 
73
+ def camel_case_acronyms=(input)
74
+ arr = sanitise(input)
75
+
76
+ @camel_case_acronyms = arr.map { |word| word.is_a?(String) ? word.to_s.downcase : nil }.compact.uniq
77
+ end
78
+
71
79
  def reset!
72
80
  @underscore = [' ', '::', '-']
73
81
  @remove = %w[' ( ) , . : " ! @ £ $ % ^ & * / { } [ ] < > ; = #]
74
82
  @omit = []
75
83
  @handle_camel_case = true
84
+ @camel_case_acronyms = []
76
85
  end
77
86
 
78
87
  private
79
88
 
80
- # Converts chars into Array of Strings
89
+ # Sanitises the input into an Array of Strings
81
90
  #
82
- # @param arg Arg to be converted
91
+ # @param input [String, Array] to be converted
83
92
  #
84
93
  # @return Array of Strings
85
94
  #
86
95
  # @raise [ArgumentError] if the arg is not either a String or an Array.
87
- def sanitise_chars(arg)
88
- raise ArgumentError 'needs to be a String or an Array of characters' unless [String, Array].include?(arg.class)
96
+ def sanitise(input)
97
+ raise ArgumentError 'needs to be a String or an Array' unless [String, Array].include?(input.class)
89
98
 
90
- arg.respond_to?(:chars) ? arg.chars : arg.map(&:to_s)
99
+ if input.respond_to?(:chars)
100
+ input.chars
101
+ else
102
+ input.map { |obj| obj.respond_to?(:to_s) ? obj.to_s.downcase : nil }.compact.uniq
103
+ end
91
104
  end
92
105
  end
93
106
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleSymbolize
4
- VERSION = '4.1.0'
4
+ VERSION = '6.0.0'
5
5
  end
@@ -31,19 +31,20 @@ module SimpleSymbolize
31
31
  #
32
32
  # @example Symbolize a string using the symbolize method
33
33
  # SimpleSymbolize.symbolize("hello world!") #=> :hello_world
34
- def self.symbolize(obj)
35
- return obj unless obj.respond_to?(:to_s)
36
- return obj if [Hash, Array, NilClass].include?(obj.class)
37
- return obj if obj.respond_to?(:empty?) && obj.empty?
34
+ def self.symbolize(obj, strip_chars: true)
35
+ return obj unless valid_input?(obj)
38
36
 
39
37
  obj = if SimpleSymbolize.translations.handle_camel_case
40
- snakeize(obj)
38
+ snakeize(obj, strip_chars:)
41
39
  else
42
40
  obj.to_s
43
41
  .downcase
44
42
  .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
45
- .gsub(Regexp.union(SimpleSymbolize.translations.remove), '')
43
+ .then do |str|
44
+ strip_chars?(strip_chars) ? str.gsub(Regexp.union(SimpleSymbolize.translations.remove), '') : str
45
+ end
46
46
  end
47
+
47
48
  obj.to_sym
48
49
  end
49
50
 
@@ -53,12 +54,10 @@ module SimpleSymbolize
53
54
  #
54
55
  # @example Elementize a string using the elementize method
55
56
  # SimpleSymbolize.elementize("hello world!") #=> "helloWorld"
56
- def self.elementize(obj)
57
- return obj unless obj.respond_to?(:to_s)
58
- return obj if [Hash, Array, NilClass].include?(obj.class)
59
- return obj if obj.respond_to?(:empty?) && obj.empty?
57
+ def self.elementize(obj, strip_chars: true)
58
+ return obj unless valid_input?(obj)
60
59
 
61
- symbolize(obj).to_s
60
+ symbolize(obj, strip_chars:).to_s
62
61
  end
63
62
 
64
63
  # Turns a String object into a camelCase Symbol.
@@ -67,15 +66,13 @@ module SimpleSymbolize
67
66
  #
68
67
  # @example Camelize a string using the camelize method
69
68
  # SimpleSymbolize.camelize("hello world!") #=> :helloWorld
70
- def self.camelize(obj)
71
- return obj unless obj.respond_to?(:to_s)
72
- return obj if [Hash, Array, NilClass].include?(obj.class)
73
- return obj if obj.respond_to?(:empty?) && obj.empty?
69
+ def self.camelize(obj, strip_chars: true)
70
+ return obj unless valid_input?(obj)
74
71
 
75
- first, *rest = elementize(obj).split('_')
72
+ first, *rest = elementize(obj, strip_chars:).split('_')
76
73
  return obj if first.nil?
77
74
 
78
- rest.size.positive? ? (first << rest.map(&:capitalize).join).to_sym : symbolize(first)
75
+ assemble_came_case_string(first, rest)
79
76
  end
80
77
 
81
78
  # Turns a String || Symbol into a snake_case Symbol
@@ -84,17 +81,56 @@ module SimpleSymbolize
84
81
  #
85
82
  # @example Snakeize an object using the snakeize method
86
83
  # SimpleSymbolize.snakeize('Hello World!') #=> :hello_world
87
- def self.snakeize(obj)
88
- return obj unless obj.respond_to?(:to_s)
89
- return obj if [Hash, Array, NilClass].include?(obj.class)
90
- return obj if obj.respond_to?(:empty?) && obj.empty?
84
+ def self.snakeize(obj, strip_chars: true)
85
+ return obj unless valid_input?(obj)
91
86
 
92
87
  obj.to_s
93
88
  .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
94
- .gsub(Regexp.union(SimpleSymbolize.translations.remove), '')
89
+ .then { |str| strip_chars?(strip_chars) ? str.gsub(Regexp.union(SimpleSymbolize.translations.remove), '') : str }
95
90
  .gsub(/([a-z\d])([A-Z])/, '\1_\2')
96
91
  .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
97
92
  .downcase
98
93
  .to_sym
99
94
  end
95
+
96
+ class << self
97
+ private
98
+
99
+ # Validates if the input object should be processed
100
+ #
101
+ # @param obj [Object] the object to validate
102
+ #
103
+ # @return [Boolean] true if the object should be processed, false otherwise
104
+ def valid_input?(obj)
105
+ return false unless [String, Symbol].include?(obj.class)
106
+ return false unless obj.respond_to?(:to_s)
107
+ return false if obj.to_s.empty?
108
+
109
+ true
110
+ end
111
+
112
+ # Abstracts the complicated building of the camelCase String
113
+ #
114
+ # @param first [String] the first part of the camelCase String
115
+ # @param rest [Array] the rest of the camelCase String
116
+ #
117
+ # @return [Symbol] camelCase representation of the input
118
+ def assemble_came_case_string(first, rest)
119
+ return symbolize(first) unless rest.size.positive?
120
+
121
+ acronyms = SimpleSymbolize.translations.camel_case_acronyms
122
+ rest = if acronyms.empty?
123
+ rest.map(&:capitalize)
124
+ else
125
+ rest.map { |word| acronyms.include?(word) ? word.upcase : word.capitalize }
126
+ end
127
+
128
+ word = first + rest.join
129
+ word.to_sym
130
+ end
131
+
132
+ def strip_chars?(obj)
133
+ obj.to_s.downcase == 'true'
134
+ end
135
+ end
100
136
  end
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.homepage = 'https://github.com/dvla/simple-symbolize'
17
17
 
18
18
  spec.license = 'MIT'
19
- spec.required_ruby_version = Gem::Requirement.new('>= 3.0')
19
+ spec.required_ruby_version = Gem::Requirement.new('>= 3.4')
20
20
 
21
21
  spec.metadata['homepage_uri'] = spec.homepage
22
22
  spec.metadata['source_code_uri'] = spec.homepage
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_symbolize
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alexo
8
8
  - Driver and Vehicle Licensing Agency (DVLA)
9
- autorequire:
10
9
  bindir: exe
11
10
  cert_chain: []
12
- date: 2025-08-01 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: 'simple_symbolize will remove special characters from a String, replacing
15
14
  whitespace with an underscore, down-casing and finally calling the #to_sym String
@@ -44,7 +43,6 @@ licenses:
44
43
  metadata:
45
44
  homepage_uri: https://github.com/dvla/simple-symbolize
46
45
  source_code_uri: https://github.com/dvla/simple-symbolize
47
- post_install_message:
48
46
  rdoc_options: []
49
47
  require_paths:
50
48
  - lib
@@ -52,15 +50,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
50
  requirements:
53
51
  - - ">="
54
52
  - !ruby/object:Gem::Version
55
- version: '3.0'
53
+ version: '3.4'
56
54
  required_rubygems_version: !ruby/object:Gem::Requirement
57
55
  requirements:
58
56
  - - ">="
59
57
  - !ruby/object:Gem::Version
60
58
  version: '0'
61
59
  requirements: []
62
- rubygems_version: 3.2.15
63
- signing_key:
60
+ rubygems_version: 4.0.10
64
61
  specification_version: 4
65
62
  summary: Turns Strings into Symbols.
66
63
  test_files: []