Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Method underscore_without_question_mark to get rid of question marks #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions lib/case_transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def underscore_cache
@underscore_cache ||= {}
end

def underscore_without_question_mark_cache
@underscore_without_question_mark ||= {}
end

# Transforms values to UpperCamelCase or PascalCase.
#
# @example:
Expand Down Expand Up @@ -80,6 +84,20 @@ def underscore(value)
end
end

# Transforms values to underscore_case and removed question marks from boolean methods.
#
# @example:
# "some-key?" => "some_key",
def underscore_without_question_mark(value)
case value
when Array then value.map { |item| underscore_without_question_mark(item) }
when Hash then value.deep_transform_keys! { |key| underscore_without_question_mark(key) }
when Symbol then underscore_without_question_mark(value.to_s).to_sym
when String then underscore_without_question_mark_cache[value] ||= value.underscore.sub(/\?\z/, '')
else value
end
end

# Returns the value unaltered
def unaltered(value)
value
Expand Down
82 changes: 82 additions & 0 deletions test/transforms/underscore_without_question_mark_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true
require 'test_helper'

describe CaseTransform do
describe 'Transforms' do
describe 'underscore_without_question_mark' do
it 'transforms to underscore_without_question_mark (snake case)' do
obj = Object.new
scenarios = [
{
value: { :"some-key?" => 'value' },
expected: { some_key: 'value' }
},
{
value: { 'some-key?' => 'value' },
expected: { 'some_key' => 'value' }
},
{
value: { SomeKey?: 'value' },
expected: { some_key: 'value' }
},
{
value: { 'SomeKey?' => 'value' },
expected: { 'some_key' => 'value' }
},
{
value: { someKey?: 'value' },
expected: { some_key: 'value' }
},
{
value: { 'someKey?' => 'value' },
expected: { 'some_key' => 'value' }
},
{
value: :"some-value?",
expected: :some_value
},
{
value: :SomeValue?,
expected: :some_value
},
{
value: :someValue?,
expected: :some_value
},
{
value: 'some-value?',
expected: 'some_value'
},
{
value: 'SomeValue?',
expected: 'some_value'
},
{
value: 'someValue?',
expected: 'some_value'
},
{
value: obj,
expected: obj
},
{
value: nil,
expected: nil
},
{
value: [
{ 'some-value?' => 'value' }
],
expected: [
{ 'some_value' => 'value' }
]
}
]
scenarios.each do |s|
result = CaseTransform.underscore_without_question_mark(s[:value])
assert_equal s[:expected], result
end
end
end
end
end