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
19 changes: 2 additions & 17 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 3.0
TargetRubyVersion: 3.2

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

Style/MixinUsage:
Enabled: false

Lint/BooleanSymbol:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/MethodLength:
Max: 25

Layout/LineLength:
Max: 130

Metrics/CyclomaticComplexity:
Max: 8
Enabled: false
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ gemspec

gem 'rake', '~> 13.3'
gem 'rspec', '~> 3.13'
gem 'rubocop', '~> 1.79'
gem 'rubocop', '~> 1.80'
gem 'simplecov', '~> 0.22'
88 changes: 76 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SimpleSymbolize

SimpleSymbolize takes a string and transforms it into a symbol. Why? Because working with symbols in Ruby makes for a
SimpleSymbolize takes a string and transforms it into a symbol. Why? Because working with symbols in Ruby makes for a
good time.

Wait, doesn't String already have a `to_sym` method?

Correct! However, this gem takes it one step further by transforming special characters and whitespace to give you a
Correct! However, this gem takes it one step further by transforming special characters and whitespace to give you a
simple easy to work with Symbol.

It works by removing special characters in a String like `'!'` and underscoring any whitespace.
Expand Down Expand Up @@ -38,17 +38,26 @@ Or install it yourself as:

## Usage

There are two ways to symbolize your String.
There are three ways to use this gem.

### Call the symbolize method and pass it your String
### SimpleSymbolize

```ruby
require 'simple_symbolize'

SimpleSymbolize.symbolize('hello world!') # => :hello_world
```

### Call the symbolize method on your String object
Available methods:

| Method | What does it do? |
|------------|------------------------------------------------------------------------------------------------------------------------------------------|
| symbolize | Returns your object as a plain old symbol |
| elementize | Returns your object as a plain old String |
| camelize | Returns your object as a plain old camelCase symbol |
| 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) | |

### Mixing into String

```ruby
require 'simple_symbolize'
Expand All @@ -58,7 +67,16 @@ String.include SimpleSymbolize::CoreExt::String
'hello world!'.simple_symbolize # => :hello_world
```

### Call the symbolize method on your Symbol object
Available methods:

| Method | What does it do? |
|------------|------------------------------------------------------------------------------------------------------------------------------------------|
| simple_symbolize | Returns your object as a plain old symbol |
| simple_elementize | Returns your object as a plain old String |
| simple_camelize | Returns your object as a plain old camelCase symbol |
| 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) | |

### Mixing into Symbol

```ruby
require 'simple_symbolize'
Expand All @@ -68,9 +86,18 @@ Symbol.include SimpleSymbolize::CoreExt::Symbol
:hello_world!.simple_symbolize # => :hello_world
```

Available methods:

| Method | What does it do? |
|------------|------------------------------------------------------------------------------------------------------------------------------------------|
| simple_symbolize | Returns your object as a plain old symbol |
| simple_elementize | Returns your object as a plain old String |
| simple_camelize | Returns your object as a plain old camelCase symbol |
| 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) | |

## Configuration

Something not underscored or removed? Or even something underscored/removed that you didn't want transformed?
Something not underscored or removed? Or even something underscored/removed that you didn't want transformed?

No sweat, you can configure this gem to underscore and remove to your hearts content!

Expand All @@ -84,20 +111,51 @@ end

## Updates!

### V5

#### Dropped support for Ruby < 3.2

Ruby 3.1 reached EOL in 2024, as such this gem no longer supports versions older than 3.2.

#### Only deals with Strings and Symbols

In previous iterations, certain non String/Symbol objects would be handled `SimpleSymbolize.symbolze(true)` returned
`:true`.

To be consistent, SimpleSymbolize now only accepts String/Symbols, everything else will be returned.

#### Uppercase acronyms when camelizing

Sometimes you want certain phrases to be in uppercase when camelizing, SimpleSymbolize now supports this.

```ruby
SimpleSymbolize.translate { |t| t.camel_case_acronym = ['gb'] }

SimpleSymbolize.camelize('from gb') # => :fromGB (without setting the camel_case_acronym this would return :fromGb)
```

---

### V4.1
### Symbol methods can now be Mixed in

SimpleSymbolize now supports mixing in the methods on the Symbol class, allowing you to call `simple_symbolize` directly on a Symbol object.
#### Symbol methods can now be Mixed in

SimpleSymbolize now supports mixing in the methods on the Symbol class, allowing you to call `simple_symbolize` directly
on a Symbol object.

```ruby
Symbol.include SimpleSymbolize::CoreExt::Symbol
:hello_world!.simple_symbolize # => :hello_world
```

---

### V4

#### String methods now need to be Mixed in

SimpleSymbolize is safe to use with other gems, particularly the popular ActiveSupport gem which SimpleSymbolize use to share
SimpleSymbolize is safe to use with other gems, particularly the popular ActiveSupport gem which SimpleSymbolize use to
share
certain methods names with.

You now need to deliberately mixin the methods on the String class:
Expand All @@ -118,9 +176,13 @@ To make them easier to spot, the method names on the String class have been pref
#### Introducing #snakeize

The `#snakeize` method will return your object in snake_case.
This is the default behaviour of the `#symbolize` method however `#snakeize` will always return thr Symbol in snake_case.
This is the default behaviour of the `#symbolize` method however `#snakeize` will always return thr Symbol in
snake_case.

---

### V3

#### String to_snake_case [DEPRECATED - replaced with `#simple_snakeize` in v4]

`#to_snake_case` extends the String class to return you your String object in snake_case format.
Expand All @@ -145,6 +207,8 @@ Arrays are now supported when configuring the gem
SimpleSymbolize.translate { |trans| trans.to_underscore = %w[!&*] }
```

---

### V2

SimpleSymbolize has got new friends!
Expand All @@ -162,7 +226,7 @@ SimpleSymbolize.elementize('hello world!') # => "hello_world"

#### Camelize

Great for working with APIs that require fields in a JSON format. Camelize clears away the clutter and returns you
Great for working with APIs that require fields in a JSON format. Camelize clears away the clutter and returns you
a Symbolized object in camelCase.

[comment]: <> (## Contributing)
Expand Down
1 change: 0 additions & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

require 'bundler/setup'
require 'simple_symbolize'
require 'pry'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
55 changes: 42 additions & 13 deletions lib/simple_symbolize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def self.translate
# @example Symbolize a string using the symbolize method
# SimpleSymbolize.symbolize("hello world!") #=> :hello_world
def self.symbolize(obj)
return obj unless obj.respond_to?(:to_s)
return obj if [Hash, Array, NilClass].include?(obj.class)
return obj if obj.respond_to?(:empty?) && obj.empty?
return obj unless valid_input?(obj)

obj = if SimpleSymbolize.translations.handle_camel_case
snakeize(obj)
Expand All @@ -54,9 +52,7 @@ def self.symbolize(obj)
# @example Elementize a string using the elementize method
# SimpleSymbolize.elementize("hello world!") #=> "helloWorld"
def self.elementize(obj)
return obj unless obj.respond_to?(:to_s)
return obj if [Hash, Array, NilClass].include?(obj.class)
return obj if obj.respond_to?(:empty?) && obj.empty?
return obj unless valid_input?(obj)

symbolize(obj).to_s
end
Expand All @@ -68,14 +64,12 @@ def self.elementize(obj)
# @example Camelize a string using the camelize method
# SimpleSymbolize.camelize("hello world!") #=> :helloWorld
def self.camelize(obj)
return obj unless obj.respond_to?(:to_s)
return obj if [Hash, Array, NilClass].include?(obj.class)
return obj if obj.respond_to?(:empty?) && obj.empty?
return obj unless valid_input?(obj)

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

rest.size.positive? ? (first << rest.map(&:capitalize).join).to_sym : symbolize(first)
assemble_came_case_string(first, rest)
end

# Turns a String || Symbol into a snake_case Symbol
Expand All @@ -85,9 +79,7 @@ def self.camelize(obj)
# @example Snakeize an object using the snakeize method
# SimpleSymbolize.snakeize('Hello World!') #=> :hello_world
def self.snakeize(obj)
return obj unless obj.respond_to?(:to_s)
return obj if [Hash, Array, NilClass].include?(obj.class)
return obj if obj.respond_to?(:empty?) && obj.empty?
return obj unless valid_input?(obj)

obj.to_s
.gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
Expand All @@ -97,4 +89,41 @@ def self.snakeize(obj)
.downcase
.to_sym
end

class << self
private

# Validates if the input object should be processed
#
# @param obj [Object] the object to validate
#
# @return [Boolean] true if the object should be processed, false otherwise
def valid_input?(obj)
return false unless [String, Symbol].include?(obj.class)
return false unless obj.respond_to?(:to_s)
return false if obj.to_s.empty?

true
end

# Abstracts the complicated building of the camelCase String
#
# @param first [String] the first part of the camelCase String
# @param rest [Array] the rest of the camelCase String
#
# @return [Symbol] camelCase representation of the input
def assemble_came_case_string(first, rest)
return symbolize(first) unless rest.size.positive?

acronyms = SimpleSymbolize.translations.camel_case_acronyms
rest = if acronyms.empty?
rest.map(&:capitalize)
else
rest.map { |word| acronyms.include?(word) ? word.upcase : word.capitalize }
end

word = first + rest.join
word.to_sym
end
end
end
Loading