Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [ '3.2', '3.3', '3.4' ]
ruby-version: [ '3.4', '4.0' ]

steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ruby_gem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [ '3.2', '3.3', '3.4' ]
ruby-version: [ '3.4', '4.0' ]

steps:
- uses: actions/checkout@v4
Expand All @@ -32,10 +32,10 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up Ruby 3.4
- name: Set up Ruby 4.0
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.4
ruby-version: 4.0
bundler-cache: true

- name: Publish to RubyGems
Expand Down
10 changes: 8 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 3.2
TargetRubyVersion: 3.4

Gemspec/DevelopmentDependencies:
EnforcedStyle: gemspec
Expand All @@ -15,4 +15,10 @@ Metrics/BlockLength:
- spec/**/*.rb

Style/MixinUsage:
Enabled: false
Enabled: false

Metrics/AbcSize:
Max: 20

Metrics/MethodLength:
Max: 20
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.5
ruby-4.0.5
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in simple_symbolize.gemspec
gemspec

gem 'rake', '~> 13.3'
gem 'rake', '~> 13.4'
gem 'rspec', '~> 3.13'
gem 'rubocop', '~> 1.80'
gem 'rubocop', '~> 1.88'
gem 'simplecov', '~> 0.22'

gem 'irb', '~> 1.18'
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ end

## Updates!

### V6.0.0

#### Dropped support for Ruby < 3.4

Ruby 3.2 reached EOL in 2026, and Ruby 3.3 will reach EOL in 2027.
As such this gem no longer supports versions older than 3.4.

#### Inline configuration

You can now prevent chars being removed inline:

```ruby
SimpleSymbolize.symbolize('Hello World!', strip_chars: false)
# => :hello_world!
```

### V5

#### Dropped support for Ruby < 3.2
Expand Down
25 changes: 16 additions & 9 deletions lib/simple_symbolize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ def self.translate
#
# @example Symbolize a string using the symbolize method
# SimpleSymbolize.symbolize("hello world!") #=> :hello_world
def self.symbolize(obj)
def self.symbolize(obj, strip_chars: true)
return obj unless valid_input?(obj)

obj = if SimpleSymbolize.translations.handle_camel_case
snakeize(obj)
snakeize(obj, strip_chars:)
else
obj.to_s
.downcase
.gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
.gsub(Regexp.union(SimpleSymbolize.translations.remove), '')
.then do |str|
strip_chars?(strip_chars) ? str.gsub(Regexp.union(SimpleSymbolize.translations.remove), '') : str
end
end

obj.to_sym
end

Expand All @@ -51,10 +54,10 @@ def self.symbolize(obj)
#
# @example Elementize a string using the elementize method
# SimpleSymbolize.elementize("hello world!") #=> "helloWorld"
def self.elementize(obj)
def self.elementize(obj, strip_chars: true)
return obj unless valid_input?(obj)

symbolize(obj).to_s
symbolize(obj, strip_chars:).to_s
end

# Turns a String object into a camelCase Symbol.
Expand All @@ -63,10 +66,10 @@ def self.elementize(obj)
#
# @example Camelize a string using the camelize method
# SimpleSymbolize.camelize("hello world!") #=> :helloWorld
def self.camelize(obj)
def self.camelize(obj, strip_chars: true)
return obj unless valid_input?(obj)

first, *rest = elementize(obj).split('_')
first, *rest = elementize(obj, strip_chars:).split('_')
return obj if first.nil?

assemble_came_case_string(first, rest)
Expand All @@ -78,12 +81,12 @@ def self.camelize(obj)
#
# @example Snakeize an object using the snakeize method
# SimpleSymbolize.snakeize('Hello World!') #=> :hello_world
def self.snakeize(obj)
def self.snakeize(obj, strip_chars: true)
return obj unless valid_input?(obj)

obj.to_s
.gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
.gsub(Regexp.union(SimpleSymbolize.translations.remove), '')
.then { |str| strip_chars?(strip_chars) ? str.gsub(Regexp.union(SimpleSymbolize.translations.remove), '') : str }
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.downcase
Expand Down Expand Up @@ -125,5 +128,9 @@ def assemble_came_case_string(first, rest)
word = first + rest.join
word.to_sym
end

def strip_chars?(obj)
obj.to_s.downcase == 'true'
end
end
end
16 changes: 8 additions & 8 deletions lib/simple_symbolize/core_ext/string/symbolize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ module CoreExt
module String
# @example Symbolize a string using the String object method
# "hello world!".symbolize #=> :hello_world
def simple_symbolize
SimpleSymbolize.symbolize(self)
def simple_symbolize(strip_chars: true)
SimpleSymbolize.symbolize(self, strip_chars:)
end

# @example Turns a String into a camelCase Symbol
# "Hello World".simple_camelize => :helloWorld
def simple_camelize
SimpleSymbolize.camelize(self)
def simple_camelize(strip_chars: true)
SimpleSymbolize.camelize(self, strip_chars:)
end

# @example Symbolizes a String then calls #to_s
# "helloWorld".simple_elementize => 'hello_word'
def simple_elementize
SimpleSymbolize.elementize(self)
def simple_elementize(strip_chars: true)
SimpleSymbolize.elementize(self, strip_chars:)
end

# @example Turns a String into it's snake_case equivalent
# "helloWorld".simple_snakeize => 'hello_word'
def simple_snakeize
SimpleSymbolize.snakeize(self)
def simple_snakeize(strip_chars: true)
SimpleSymbolize.snakeize(self, strip_chars:)
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/simple_symbolize/core_ext/symbol/symbolize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ module CoreExt
module Symbol
# @example Symbolize a string using the String object method
# :hello_world!.symbolize #=> :hello_world
def simple_symbolize
SimpleSymbolize.symbolize(self)
def simple_symbolize(strip_chars: true)
SimpleSymbolize.symbolize(self, strip_chars:)
end

# @example Turns a String into a camelCase Symbol
# :hello_world.simple_camelize => :helloWorld
def simple_camelize
SimpleSymbolize.camelize(self)
def simple_camelize(strip_chars: true)
SimpleSymbolize.camelize(self, strip_chars:)
end

# @example Symbolizes a String then calls #to_s
# :helloWorld!.simple_elementize => 'hello_word'
def simple_elementize
SimpleSymbolize.elementize(self)
def simple_elementize(strip_chars: true)
SimpleSymbolize.elementize(self, strip_chars:)
end

# @example Turns a String into it's snake_case equivalent
# :helloWorld.simple_snakeize => :hello_word
def simple_snakeize
SimpleSymbolize.snakeize(self)
def simple_snakeize(strip_chars: true)
SimpleSymbolize.snakeize(self, strip_chars:)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_symbolize/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SimpleSymbolize
VERSION = '5.0.0'
VERSION = '6.0.0'
end
2 changes: 1 addition & 1 deletion simple_symbolize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/dvla/simple-symbolize'

spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 3.2')
spec.required_ruby_version = Gem::Requirement.new('>= 3.4')

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
Expand Down
18 changes: 18 additions & 0 deletions spec/camelize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,22 @@
SimpleSymbolize.translations.camel_case_acronyms = ['gb']
expect(SimpleSymbolize.camelize('from gb')).to eq(:fromGB)
end

context 'strip_chars option' do
it 'strips chars by default' do
expect(SimpleSymbolize.camelize('Hello World!')).to eq(:helloWorld)
end

it 'does not strip chars when strip_chars is false' do
expect(SimpleSymbolize.camelize('Hello World!', strip_chars: false)).to eq(:helloWorld!)
end

it 'continues to strip chars when strip_chars is true' do
expect(SimpleSymbolize.camelize('Hello World!', strip_chars: true)).to eq(:helloWorld)
end

it 'does not strip chars when strip_chars is not a boolean' do
expect(SimpleSymbolize.camelize('Hello World!', strip_chars: 'not_a_boolean')).to eq(:helloWorld!)
end
end
end
16 changes: 16 additions & 0 deletions spec/core_ext/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
it 'symbolizes the String' do
expect('This is quite a test'.simple_symbolize).to eq(:this_is_quite_a_test)
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_symbolize(strip_chars: false)).to eq(:this_is_quite_a_test!)
end
end

context 'simple_snakeize method' do
Expand All @@ -19,6 +23,10 @@
it 'returns a snake_case Symbol' do
expect('ThisIsQuiteATest'.simple_snakeize).to eq(:this_is_quite_a_test)
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_snakeize(strip_chars: false)).to eq(:this_is_quite_a_test!)
end
end

context 'simple_elementize method' do
Expand All @@ -29,6 +37,10 @@
it 'elementizes the String' do
expect('This is quite a test'.simple_elementize).to eq('this_is_quite_a_test')
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_elementize(strip_chars: false)).to eq('this_is_quite_a_test!')
end
end

context 'simple_camelize method' do
Expand All @@ -39,5 +51,9 @@
it 'returns a snake_case Symbol' do
expect('ThisIsQuiteATest'.simple_camelize).to eq(:thisIsQuiteATest)
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_camelize(strip_chars: false)).to eq(:thisIsQuiteATest!)
end
end
end
16 changes: 16 additions & 0 deletions spec/core_ext/symbol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
it 'symbolizes the String' do
expect('This is quite a test'.simple_symbolize).to eq(:this_is_quite_a_test)
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_symbolize(strip_chars: false)).to eq(:this_is_quite_a_test!)
end
end

context 'simple_snakeize method' do
Expand All @@ -19,6 +23,10 @@
it 'returns a snake_case Symbol' do
expect('ThisIsQuiteATest'.simple_snakeize).to eq(:this_is_quite_a_test)
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_snakeize(strip_chars: false)).to eq(:this_is_quite_a_test!)
end
end

context 'simple_elementize method' do
Expand All @@ -29,6 +37,10 @@
it 'elementizes the String' do
expect('This is quite a test'.simple_elementize).to eq('this_is_quite_a_test')
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_elementize(strip_chars: false)).to eq('this_is_quite_a_test!')
end
end

context 'simple_camelize method' do
Expand All @@ -39,5 +51,9 @@
it 'returns a snake_case Symbol' do
expect('ThisIsQuiteATest'.simple_camelize).to eq(:thisIsQuiteATest)
end

it 'allows for a strip_chars option' do
expect('This is quite a test!'.simple_camelize(strip_chars: false)).to eq(:thisIsQuiteATest!)
end
end
end
18 changes: 18 additions & 0 deletions spec/elementize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@
expect(SimpleSymbolize.elementize([])).to eq([])
expect(SimpleSymbolize.elementize('')).to eq('')
end

context 'strip_chars option' do
it 'strips chars by default' do
expect(SimpleSymbolize.elementize('Hello World!')).to eq('hello_world')
end

it 'does not strip chars when strip_chars is false' do
expect(SimpleSymbolize.elementize('Hello World!', strip_chars: false)).to eq('hello_world!')
end

it 'continues to strip chars when strip_chars is true' do
expect(SimpleSymbolize.elementize('Hello World!', strip_chars: true)).to eq('hello_world')
end

it 'does not strip chars when strip_chars is not a boolean' do
expect(SimpleSymbolize.elementize('Hello World!', strip_chars: 'not_a_boolean')).to eq('hello_world!')
end
end
end
Loading
Loading