diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 754f5fd..dcbcbe3 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -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 diff --git a/.github/workflows/ruby_gem.yml b/.github/workflows/ruby_gem.yml index 70dfe38..6fd72f1 100644 --- a/.github/workflows/ruby_gem.yml +++ b/.github/workflows/ruby_gem.yml @@ -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 @@ -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 diff --git a/.rubocop.yml b/.rubocop.yml index 2cd31fb..25f22b4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,7 @@ AllCops: NewCops: enable SuggestExtensions: false - TargetRubyVersion: 3.2 + TargetRubyVersion: 3.4 Gemspec/DevelopmentDependencies: EnforcedStyle: gemspec @@ -15,4 +15,10 @@ Metrics/BlockLength: - spec/**/*.rb Style/MixinUsage: - Enabled: false \ No newline at end of file + Enabled: false + +Metrics/AbcSize: + Max: 20 + +Metrics/MethodLength: + Max: 20 \ No newline at end of file diff --git a/.ruby-version b/.ruby-version index 3ec370e..6bbb7ff 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.4.5 \ No newline at end of file +ruby-4.0.5 \ No newline at end of file diff --git a/Gemfile b/Gemfile index 05f5590..08439c2 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/README.md b/README.md index 55c9bc2..3f8ea1c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/simple_symbolize.rb b/lib/simple_symbolize.rb index 8f0cec0..74f5357 100644 --- a/lib/simple_symbolize.rb +++ b/lib/simple_symbolize.rb @@ -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 @@ -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. @@ -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) @@ -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 @@ -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 diff --git a/lib/simple_symbolize/core_ext/string/symbolize.rb b/lib/simple_symbolize/core_ext/string/symbolize.rb index ba7fafd..071cf59 100644 --- a/lib/simple_symbolize/core_ext/string/symbolize.rb +++ b/lib/simple_symbolize/core_ext/string/symbolize.rb @@ -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 diff --git a/lib/simple_symbolize/core_ext/symbol/symbolize.rb b/lib/simple_symbolize/core_ext/symbol/symbolize.rb index b949341..6e60a29 100644 --- a/lib/simple_symbolize/core_ext/symbol/symbolize.rb +++ b/lib/simple_symbolize/core_ext/symbol/symbolize.rb @@ -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 diff --git a/lib/simple_symbolize/version.rb b/lib/simple_symbolize/version.rb index 25748fc..415c0d7 100644 --- a/lib/simple_symbolize/version.rb +++ b/lib/simple_symbolize/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module SimpleSymbolize - VERSION = '5.0.0' + VERSION = '6.0.0' end diff --git a/simple_symbolize.gemspec b/simple_symbolize.gemspec index dbf2e38..8d82844 100644 --- a/simple_symbolize.gemspec +++ b/simple_symbolize.gemspec @@ -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 diff --git a/spec/camelize_spec.rb b/spec/camelize_spec.rb index 8346f3f..339c4bf 100644 --- a/spec/camelize_spec.rb +++ b/spec/camelize_spec.rb @@ -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 diff --git a/spec/core_ext/string_spec.rb b/spec/core_ext/string_spec.rb index a3c6c20..a405692 100644 --- a/spec/core_ext/string_spec.rb +++ b/spec/core_ext/string_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/core_ext/symbol_spec.rb b/spec/core_ext/symbol_spec.rb index 63bc57c..db5c8e4 100644 --- a/spec/core_ext/symbol_spec.rb +++ b/spec/core_ext/symbol_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/elementize_spec.rb b/spec/elementize_spec.rb index 09b170f..5fd4856 100644 --- a/spec/elementize_spec.rb +++ b/spec/elementize_spec.rb @@ -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 diff --git a/spec/simple_symbolize_spec.rb b/spec/simple_symbolize_spec.rb index 8d90f21..66922b7 100644 --- a/spec/simple_symbolize_spec.rb +++ b/spec/simple_symbolize_spec.rb @@ -196,4 +196,22 @@ expect(SimpleSymbolize.translations.underscore).not_to include('^') end end + + context 'strip_chars option' do + it 'strips chars by default' do + expect(SimpleSymbolize.symbolize('Hello World!')).to eq(:hello_world) + end + + it 'does not strip chars when strip_chars is false' do + expect(SimpleSymbolize.symbolize('Hello World!', strip_chars: false)).to eq(:hello_world!) + end + + it 'continues to strip chars when strip_chars is true' do + expect(SimpleSymbolize.symbolize('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.symbolize('Hello World!', strip_chars: 'not_a_boolean')).to eq(:hello_world!) + end + end end diff --git a/spec/snakeize_spec.rb b/spec/snakeize_spec.rb index 00673e4..67cf1ce 100644 --- a/spec/snakeize_spec.rb +++ b/spec/snakeize_spec.rb @@ -15,4 +15,22 @@ expect(SimpleSymbolize.snakeize([])).to eq([]) expect(SimpleSymbolize.snakeize('')).to eq('') end + + context 'strip_chars option' do + it 'strips chars by default' do + expect(SimpleSymbolize.snakeize('Hello World!')).to eq(:hello_world) + end + + it 'does not strip chars when strip_chars is false' do + expect(SimpleSymbolize.snakeize('Hello World!', strip_chars: false)).to eq(:hello_world!) + end + + it 'continues to strip chars when strip_chars is true' do + expect(SimpleSymbolize.snakeize('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.snakeize('Hello World!', strip_chars: 'not_a_boolean')).to eq(:hello_world!) + end + end end