Skip to content

Commit

Permalink
use benchmarked methods
Browse files Browse the repository at this point in the history
  • Loading branch information
buermann committed Oct 16, 2022
1 parent bc60279 commit 843ac21
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ AllCops:
Metrics/CyclomaticComplexity:
Max: 12

Style/PerlBackrefs:
Enabled: false

6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
0.1.0 10/14/2022
================

Re-using ActiveSupport::Inflector's camelize and underscore methods converts "::" to "/" and "/" to "::", which might be useful in Rails' internals, but in the context of a Grape API it corrupts the swagger docs.
ActiveSupport::Inflector's camelize and underscore methods convert "::" to "/" and "/" to "::", which might be useful in Rails' internals, but in the context of a Grape API it corrupts the swagger docs' "paths" objects.

Implement our own camelize and underscore methods that leave "::" and "/" alone, rather than relying on ActiveSupport.
Implement our own camel and snake case methods that leave "::" and "/" unmolested, rather than relying on ActiveSupport, in which case we'll drop that dependency altogether by removing the (trivially re-implemented) indifferent access functionality.

We are not re-implmenting ActiveSupport's acronym inflections. Yet.

Replace travis etc. with github actions.

Expand Down
31 changes: 15 additions & 16 deletions lib/camel_snake_keys.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Convert the keys of hashes in nested structures to camel or underscore case.
# Convert the keys of hashes in nested structures to camel or snake case.
module CamelSnakeKeys
[Hash, Array].each do |klass|
refine klass do
Expand All @@ -15,27 +15,26 @@ def with_snake_keys
end

class << self
def camelize(obj)
return unless obj

str = obj.to_s.split(/[-_]+/).map { |w| w[0].upcase! + w[1..-1] }.join
str[0].downcase! + str[1..-1]
def camelcase(obj)
string = +obj.to_s # unfreeze whatever it might be with a leading +
string.sub!(/^([A-Z])/) { $1.downcase }
string.gsub!(/_([a-z\d])/) { $1.capitalize }
string
end

def underscore(obj)
return unless obj

obj.to_s.gsub('-', '_').split(/([A-Z])/).map do |syl|
syl =~ /^[A-Z]/ ? "_#{syl.downcase!}" : syl
end.join.sub(/^_/, '')
def snakecase(obj)
string = +obj.to_s
string.gsub!(/([A-Z])/) { "_#{$1}" }
string.downcase!
string
end

def if_underscore(obj)
case obj
when Symbol
underscore(obj.to_s).to_sym
snakecase(obj.to_s).to_sym
when String
underscore(obj)
snakecase(obj)
else
obj
end
Expand All @@ -44,9 +43,9 @@ def if_underscore(obj)
def if_camelize(obj)
case obj
when Symbol
camelize(obj.to_s).to_sym
camelcase(obj.to_s).to_sym
when String
camelize(obj)
camelcase(obj)
else
obj
end
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/camel_snake_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@
using CamelSnakeKeys

RSpec.describe Enumerable do

context 'camelize and underscore' do
let(:underscore_to_camel) {
{
"product" => "product",
"special_guest" => "specialGuest",
"application_controller" => "applicationController",
"area51_controller" => "area51Controller",
product: "product",
special_guest: "specialGuest",
application_controller: "applicationController",
area51_controller: "area51Controller",
"camel_snake/keys" => "camelSnake/keys",
"camel_snake::keys" => "camelSnake::keys"
}
}
it "converts strings and symbols to camel and snake case strings as expected" do
underscore_to_camel.each do |k,v|
CamelSnakeKeys.camelcase(k).should eq v.to_s
CamelSnakeKeys.snakecase(v).should eq k.to_s
end
end

it "handles nils gracefully" do
CamelSnakeKeys.camelcase(nil).should eq ""
CamelSnakeKeys.snakecase(nil).should eq ""
end
end

context 'arrays' do
let(:snaked) do
[[{ true => false, 1 => 1.2, 1.2 => 1, nil => 2, :foo_bar => 1,
Expand Down

0 comments on commit 843ac21

Please sign in to comment.