simple_symbolize 4.1.0 → 5.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: 1da0a7a552ad0fd3c5901bbf7141a0cb03437b96379df5506690d65f1424592b
4
+ data.tar.gz: 4725b7b1c52b62649b683369856d9eb37d66b9569db9e283acfaa426fdad4be9
5
5
  SHA512:
6
- metadata.gz: da85cabac7e7f368cfd7a0366dffa7642e6f0b27f2cbac49b548264df55f82eb715d74cf80e2113a172b7451b2496a86ddcbc13ef818d66e8b7f72aa8df80ff1
7
- data.tar.gz: 82a0bd3a48cecbf2ca49607dd136cc8dc46a3babb761507b88a74043a494f396fbe6e7ecc53393c5a2d065e2f8d0302ccfdf80dfe6cb2412d3188091f15d2f44
6
+ metadata.gz: ebaeea1ca71fb7532b98e6b9c020ac35432e0c17eeedb908081f6e7dc0656e38471a9bf98f37fee1f87cafb1e4d185d49d51ad6c4f4b7c89b79e985a0b127c3a
7
+ data.tar.gz: 5cc276021f11076c11f3ac704388b67e37451acdc0d0eab68bc11f6fcf007c94d321e6d5acf253e5ff7382e943008122a4f9ca835bf709db05b125ad34da7fd6
@@ -21,7 +21,7 @@ jobs:
21
21
  ruby-version: [ '3.2', '3.3', '3.4' ]
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:
@@ -12,7 +12,7 @@ jobs:
12
12
  ruby-version: [ '3.2', '3.3', '3.4' ]
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 3.4
36
36
  uses: ruby/setup-ruby@v1
37
37
  with:
38
- ruby-version: 3.0.1
38
+ ruby-version: 3.4
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.2
5
5
 
6
6
  Gemspec/DevelopmentDependencies:
7
7
  EnforcedStyle: gemspec
@@ -15,19 +15,4 @@ Metrics/BlockLength:
15
15
  - spec/**/*.rb
16
16
 
17
17
  Style/MixinUsage:
18
- Enabled: false
19
-
20
- Lint/BooleanSymbol:
21
- Enabled: false
22
-
23
- Metrics/AbcSize:
24
- Enabled: false
25
-
26
- Metrics/MethodLength:
27
- Max: 25
28
-
29
- Layout/LineLength:
30
- Max: 130
31
-
32
- Metrics/CyclomaticComplexity:
33
- Max: 8
18
+ Enabled: false
data/Gemfile CHANGED
@@ -7,5 +7,5 @@ gemspec
7
7
 
8
8
  gem 'rake', '~> 13.3'
9
9
  gem 'rspec', '~> 3.13'
10
- gem 'rubocop', '~> 1.79'
10
+ gem 'rubocop', '~> 1.80'
11
11
  gem 'simplecov', '~> 0.22'
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,51 @@ end
84
111
 
85
112
  ## Updates!
86
113
 
114
+ ### V5
115
+
116
+ #### Dropped support for Ruby < 3.2
117
+
118
+ Ruby 3.1 reached EOL in 2024, as such this gem no longer supports versions older than 3.2.
119
+
120
+ #### Only deals with Strings and Symbols
121
+
122
+ In previous iterations, certain non String/Symbol objects would be handled `SimpleSymbolize.symbolze(true)` returned
123
+ `:true`.
124
+
125
+ To be consistent, SimpleSymbolize now only accepts String/Symbols, everything else will be returned.
126
+
127
+ #### Uppercase acronyms when camelizing
128
+
129
+ Sometimes you want certain phrases to be in uppercase when camelizing, SimpleSymbolize now supports this.
130
+
131
+ ```ruby
132
+ SimpleSymbolize.translate { |t| t.camel_case_acronym = ['gb'] }
133
+
134
+ SimpleSymbolize.camelize('from gb') # => :fromGB (without setting the camel_case_acronym this would return :fromGb)
135
+ ```
136
+
137
+ ---
138
+
87
139
  ### V4.1
88
- ### Symbol methods can now be Mixed in
89
140
 
90
- SimpleSymbolize now supports mixing in the methods on the Symbol class, allowing you to call `simple_symbolize` directly on a Symbol object.
141
+ #### Symbol methods can now be Mixed in
142
+
143
+ SimpleSymbolize now supports mixing in the methods on the Symbol class, allowing you to call `simple_symbolize` directly
144
+ on a Symbol object.
91
145
 
92
146
  ```ruby
93
147
  Symbol.include SimpleSymbolize::CoreExt::Symbol
94
148
  :hello_world!.simple_symbolize # => :hello_world
95
149
  ```
96
150
 
151
+ ---
152
+
97
153
  ### V4
154
+
98
155
  #### String methods now need to be Mixed in
99
156
 
100
- SimpleSymbolize is safe to use with other gems, particularly the popular ActiveSupport gem which SimpleSymbolize use to share
157
+ SimpleSymbolize is safe to use with other gems, particularly the popular ActiveSupport gem which SimpleSymbolize use to
158
+ share
101
159
  certain methods names with.
102
160
 
103
161
  You now need to deliberately mixin the methods on the String class:
@@ -118,9 +176,13 @@ To make them easier to spot, the method names on the String class have been pref
118
176
  #### Introducing #snakeize
119
177
 
120
178
  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.
179
+ This is the default behaviour of the `#symbolize` method however `#snakeize` will always return thr Symbol in
180
+ snake_case.
181
+
182
+ ---
122
183
 
123
184
  ### V3
185
+
124
186
  #### String to_snake_case [DEPRECATED - replaced with `#simple_snakeize` in v4]
125
187
 
126
188
  `#to_snake_case` extends the String class to return you your String object in snake_case format.
@@ -145,6 +207,8 @@ Arrays are now supported when configuring the gem
145
207
  SimpleSymbolize.translate { |trans| trans.to_underscore = %w[!&*] }
146
208
  ```
147
209
 
210
+ ---
211
+
148
212
  ### V2
149
213
 
150
214
  SimpleSymbolize has got new friends!
@@ -162,7 +226,7 @@ SimpleSymbolize.elementize('hello world!') # => "hello_world"
162
226
 
163
227
  #### Camelize
164
228
 
165
- Great for working with APIs that require fields in a JSON format. Camelize clears away the clutter and returns you
229
+ Great for working with APIs that require fields in a JSON format. Camelize clears away the clutter and returns you
166
230
  a Symbolized object in camelCase.
167
231
 
168
232
  [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,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 = '5.0.0'
5
5
  end
@@ -32,9 +32,7 @@ module SimpleSymbolize
32
32
  # @example Symbolize a string using the symbolize method
33
33
  # SimpleSymbolize.symbolize("hello world!") #=> :hello_world
34
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?
35
+ return obj unless valid_input?(obj)
38
36
 
39
37
  obj = if SimpleSymbolize.translations.handle_camel_case
40
38
  snakeize(obj)
@@ -54,9 +52,7 @@ module SimpleSymbolize
54
52
  # @example Elementize a string using the elementize method
55
53
  # SimpleSymbolize.elementize("hello world!") #=> "helloWorld"
56
54
  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?
55
+ return obj unless valid_input?(obj)
60
56
 
61
57
  symbolize(obj).to_s
62
58
  end
@@ -68,14 +64,12 @@ module SimpleSymbolize
68
64
  # @example Camelize a string using the camelize method
69
65
  # SimpleSymbolize.camelize("hello world!") #=> :helloWorld
70
66
  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?
67
+ return obj unless valid_input?(obj)
74
68
 
75
69
  first, *rest = elementize(obj).split('_')
76
70
  return obj if first.nil?
77
71
 
78
- rest.size.positive? ? (first << rest.map(&:capitalize).join).to_sym : symbolize(first)
72
+ assemble_came_case_string(first, rest)
79
73
  end
80
74
 
81
75
  # Turns a String || Symbol into a snake_case Symbol
@@ -85,9 +79,7 @@ module SimpleSymbolize
85
79
  # @example Snakeize an object using the snakeize method
86
80
  # SimpleSymbolize.snakeize('Hello World!') #=> :hello_world
87
81
  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?
82
+ return obj unless valid_input?(obj)
91
83
 
92
84
  obj.to_s
93
85
  .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
@@ -97,4 +89,41 @@ module SimpleSymbolize
97
89
  .downcase
98
90
  .to_sym
99
91
  end
92
+
93
+ class << self
94
+ private
95
+
96
+ # Validates if the input object should be processed
97
+ #
98
+ # @param obj [Object] the object to validate
99
+ #
100
+ # @return [Boolean] true if the object should be processed, false otherwise
101
+ def valid_input?(obj)
102
+ return false unless [String, Symbol].include?(obj.class)
103
+ return false unless obj.respond_to?(:to_s)
104
+ return false if obj.to_s.empty?
105
+
106
+ true
107
+ end
108
+
109
+ # Abstracts the complicated building of the camelCase String
110
+ #
111
+ # @param first [String] the first part of the camelCase String
112
+ # @param rest [Array] the rest of the camelCase String
113
+ #
114
+ # @return [Symbol] camelCase representation of the input
115
+ def assemble_came_case_string(first, rest)
116
+ return symbolize(first) unless rest.size.positive?
117
+
118
+ acronyms = SimpleSymbolize.translations.camel_case_acronyms
119
+ rest = if acronyms.empty?
120
+ rest.map(&:capitalize)
121
+ else
122
+ rest.map { |word| acronyms.include?(word) ? word.upcase : word.capitalize }
123
+ end
124
+
125
+ word = first + rest.join
126
+ word.to_sym
127
+ end
128
+ end
100
129
  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.2')
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: 5.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.2'
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: 3.6.9
64
61
  specification_version: 4
65
62
  summary: Turns Strings into Symbols.
66
63
  test_files: []