From cba2ff48eee4cef9d537fc77ac7b0e1e29972626 Mon Sep 17 00:00:00 2001 From: Daniel Trierweiler Date: Tue, 21 Feb 2017 10:03:35 +0100 Subject: [PATCH] New Method `underscore_without_question_mark` to get rid of question marks in boolean methods --- lib/case_transform.rb | 18 ++++ .../underscore_without_question_mark_test.rb | 82 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 test/transforms/underscore_without_question_mark_test.rb diff --git a/lib/case_transform.rb b/lib/case_transform.rb index 57b8be6..6e3c1c1 100644 --- a/lib/case_transform.rb +++ b/lib/case_transform.rb @@ -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: @@ -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 diff --git a/test/transforms/underscore_without_question_mark_test.rb b/test/transforms/underscore_without_question_mark_test.rb new file mode 100644 index 0000000..3fb12be --- /dev/null +++ b/test/transforms/underscore_without_question_mark_test.rb @@ -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