From cea54de46f1f65fad7b104be085d67976472c6f4 Mon Sep 17 00:00:00 2001 From: Diego Marrufo Date: Fri, 25 Apr 2025 09:54:28 +0200 Subject: [PATCH 1/4] case formatter and tests. bash aliases to count lines of code --- rb/helpers/case_formatter.rb | 41 ++++++++++++ rb/spec/case_formatter_spec.rb | 113 +++++++++++++++++++++++++++++++++ zsh/.zshrc | 8 +++ 3 files changed, 162 insertions(+) create mode 100644 rb/helpers/case_formatter.rb create mode 100644 rb/spec/case_formatter_spec.rb diff --git a/rb/helpers/case_formatter.rb b/rb/helpers/case_formatter.rb new file mode 100644 index 0000000..b3aac66 --- /dev/null +++ b/rb/helpers/case_formatter.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +# CaseFormatter allows you to convert from and to snake_case and camelCase +# It support nested elements as well, such as Arrays and Hashes +module CaseFormatter + extend ActiveSupport::Concern # TODO: import active support or define the module differently + + included do + def snake_to_camel_case(obj) + case obj + when String + obj.camelize(:lower) + when Symbol + obj.to_s.camelize(:lower).to_sym + when Hash + obj.deep_transform_keys { |key| key.to_s.camelize(:lower).to_sym } + when Array + obj.map { |item| snake_to_camel_case(item) } + else + raise ArgumentError, "Unexpected type for case conversion: #{obj.class}" + end + end + + def camel_to_snake_case(obj) + case obj + when String + obj.underscore + when Symbol + obj.to_s.underscore.to_sym + when Hash + obj.deep_transform_keys { |key| key.to_s.underscore.to_sym } + when ActionController::Parameters, ->(o) { o.class.superclass == Types::BaseInputObject } # TODO: can change to responds_to to_h + camel_to_snake_case(obj.to_h.deep_symbolize_keys) + when Array + obj.map { |item| camel_to_snake_case(item) } + else + raise ArgumentError, "Unexpected type for case conversion: #{obj.class}" + end + end + end +end diff --git a/rb/spec/case_formatter_spec.rb b/rb/spec/case_formatter_spec.rb new file mode 100644 index 0000000..d90311f --- /dev/null +++ b/rb/spec/case_formatter_spec.rb @@ -0,0 +1,113 @@ +# frozen_string_literal: true + +describe CaseFormatter do + let(:instance) { Class.new { include CaseFormatter }.new } + + describe '#snake_to_camel_case' do + context 'when input is a string' do + it 'converts snake_case to camelCase' do + expect(instance.snake_to_camel_case('hello_world')).to eq('helloWorld') + end + end + + context 'when input is a symbol' do + it 'converts snake_case to camelCase' do + expect(instance.snake_to_camel_case(:hello_world)).to eq(:helloWorld) + end + end + + context 'when input is a hash' do + it 'converts all keys from snake_case to camelCase' do + input = { first_name: 'John', last_name: 'Doe' } + expected = { firstName: 'John', lastName: 'Doe' } + expect(instance.snake_to_camel_case(input)).to eq(expected) + end + + it 'converts nested hash keys' do + input = { user_data: { first_name: 'John', address_info: { street_name: 'Main' } } } + expected = { userData: { firstName: 'John', addressInfo: { streetName: 'Main' } } } + expect(instance.snake_to_camel_case(input)).to eq(expected) + end + end + + context 'when input is an array' do + it 'converts all elements that can be converted' do + input = ['hello_world', { user_name: 'john' }, :test_case] + expected = ['helloWorld', { userName: 'john' }, :testCase] + expect(instance.snake_to_camel_case(input)).to eq(expected) + end + end + + context 'when input is another type' do + it 'raises ArgumentError for nil' do + expect { instance.snake_to_camel_case(nil) } + .to raise_error(ArgumentError, 'Unexpected type for case conversion: NilClass') + end + + it 'raises ArgumentError for numbers' do + expect { instance.snake_to_camel_case(123) } + .to raise_error(ArgumentError, 'Unexpected type for case conversion: Integer') + end + + it 'raises ArgumentError for custom objects' do + custom_object = OpenStruct.new(name: 'test') + expect { instance.snake_to_camel_case(custom_object) } + .to raise_error(ArgumentError, 'Unexpected type for case conversion: OpenStruct') + end + end + end + + describe '#camel_to_snake_case' do + context 'when input is a string' do + it 'converts camelCase to snake_case' do + expect(instance.camel_to_snake_case('helloWorld')).to eq('hello_world') + end + end + + context 'when input is a symbol' do + it 'converts camelCase to snake_case' do + expect(instance.camel_to_snake_case(:helloWorld)).to eq(:hello_world) + end + end + + context 'when input is a hash' do + it 'converts all keys from camelCase to snake_case' do + input = { firstName: 'John', lastName: 'Doe' } + expected = { first_name: 'John', last_name: 'Doe' } + expect(instance.camel_to_snake_case(input)).to eq(expected) + end + + it 'converts nested hash keys' do + input = { userData: { firstName: 'John', addressInfo: { streetName: 'Main' } } } + expected = { user_data: { first_name: 'John', address_info: { street_name: 'Main' } } } + expect(instance.camel_to_snake_case(input)).to eq(expected) + end + end + + context 'when input is an array' do + it 'converts all elements that can be converted' do + input = ['helloWorld', { userName: 'john' }, :testCase] + expected = ['hello_world', { user_name: 'john' }, :test_case] + expect(instance.camel_to_snake_case(input)).to eq(expected) + end + end + + context 'when input is another type' do + it 'raises ArgumentError for nil' do + expect { instance.snake_to_camel_case(nil) } + .to raise_error(ArgumentError, 'Unexpected type for case conversion: NilClass') + end + + it 'raises ArgumentError for numbers' do + expect { instance.snake_to_camel_case(123) } + .to raise_error(ArgumentError, 'Unexpected type for case conversion: Integer') + end + + it 'raises ArgumentError for custom objects' do + custom_object = OpenStruct.new(name: 'test') + expect { instance.snake_to_camel_case(custom_object) } + .to raise_error(ArgumentError, 'Unexpected type for case conversion: OpenStruct') + end + end + end +end diff --git a/zsh/.zshrc b/zsh/.zshrc index 6f7893e..8dd0f72 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -26,3 +26,11 @@ alias zshconfig="code ~/.zshrc" # Easter eggs alias rubydance="ruby ~/ruby_egg.rb" + +# Lines of code +alias count_rb="find app -iname "*.rb" -type f -exec cat {} \;| wc -l" +alias count_jsx="find src -iname "*.jsx" -type f -exec cat {} \;| wc -l" +alias count_js="find src -iname "*.js" -type f -exec cat {} \;| wc -l" + +# Rails +alias rails_stats="bundle exec rake stats" From 1ebd0e74dc74f1baf05223d523bc98af36a7945b Mon Sep 17 00:00:00 2001 From: Diego Marrufo Date: Wed, 30 Apr 2025 13:22:34 +0200 Subject: [PATCH 2/4] added suffix alias example --- zsh/.zshrc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zsh/.zshrc b/zsh/.zshrc index 8dd0f72..274c319 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -2,6 +2,9 @@ ZSH_THEME="gozilla" plugins=(git ssh-agent zsh-autosuggestions) +# Suffix Aliases +alias -s rb=code + # Checkout alias myscripts="cd ~/scripts" alias rbscripts="cd ~/scripts/rb" From 9bb41524efc45913d1dc502ba39150bc98ecd10e Mon Sep 17 00:00:00 2001 From: Diego Marrufo Date: Sat, 24 May 2025 17:57:13 +0200 Subject: [PATCH 3/4] remove very specific case of graphql ruby and rails params --- rb/helpers/case_formatter.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/rb/helpers/case_formatter.rb b/rb/helpers/case_formatter.rb index b3aac66..90a1088 100644 --- a/rb/helpers/case_formatter.rb +++ b/rb/helpers/case_formatter.rb @@ -29,8 +29,6 @@ def camel_to_snake_case(obj) obj.to_s.underscore.to_sym when Hash obj.deep_transform_keys { |key| key.to_s.underscore.to_sym } - when ActionController::Parameters, ->(o) { o.class.superclass == Types::BaseInputObject } # TODO: can change to responds_to to_h - camel_to_snake_case(obj.to_h.deep_symbolize_keys) when Array obj.map { |item| camel_to_snake_case(item) } else From b2d154cf681f024dec67cde0160df524a9144a15 Mon Sep 17 00:00:00 2001 From: Diego Marrufo Date: Sat, 24 May 2025 18:16:12 +0200 Subject: [PATCH 4/4] added active_support. And mocked the custom object differently in the tests --- rb/helpers/case_formatter.rb | 4 +++- rb/spec/case_formatter_spec.rb | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/rb/helpers/case_formatter.rb b/rb/helpers/case_formatter.rb index 90a1088..6cbb25b 100644 --- a/rb/helpers/case_formatter.rb +++ b/rb/helpers/case_formatter.rb @@ -1,9 +1,11 @@ # frozen_string_literal: true +require 'active_support/all' + # CaseFormatter allows you to convert from and to snake_case and camelCase # It support nested elements as well, such as Arrays and Hashes module CaseFormatter - extend ActiveSupport::Concern # TODO: import active support or define the module differently + extend ActiveSupport::Concern included do def snake_to_camel_case(obj) diff --git a/rb/spec/case_formatter_spec.rb b/rb/spec/case_formatter_spec.rb index d90311f..6af1631 100644 --- a/rb/spec/case_formatter_spec.rb +++ b/rb/spec/case_formatter_spec.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative '../helpers/case_formatter' + describe CaseFormatter do let(:instance) { Class.new { include CaseFormatter }.new } @@ -50,9 +52,9 @@ end it 'raises ArgumentError for custom objects' do - custom_object = OpenStruct.new(name: 'test') + custom_object = double expect { instance.snake_to_camel_case(custom_object) } - .to raise_error(ArgumentError, 'Unexpected type for case conversion: OpenStruct') + .to raise_error(ArgumentError) end end end @@ -104,9 +106,9 @@ end it 'raises ArgumentError for custom objects' do - custom_object = OpenStruct.new(name: 'test') + custom_object = double expect { instance.snake_to_camel_case(custom_object) } - .to raise_error(ArgumentError, 'Unexpected type for case conversion: OpenStruct') + .to raise_error(ArgumentError) end end end