From e25dfff4ec4febd68e41b2288e73299777e66a25 Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Sun, 12 Nov 2023 14:48:46 +0300 Subject: [PATCH] Adds Camelize Keys Method to Definitions --- lib/schemable/definition.rb | 4 ++++ sig/schemable/definition.rbs | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/schemable/definition.rb b/lib/schemable/definition.rb index ef601ee..8d69f86 100644 --- a/lib/schemable/definition.rb +++ b/lib/schemable/definition.rb @@ -95,5 +95,9 @@ module Schemable def model_name self.class.name.gsub('Swagger::Definitions::', '').pluralize.underscore.downcase end + + def camelize_keys(hash) + hash.deep_transform_keys { |key| key.to_s.camelize(:lower).to_sym } + end end end diff --git a/sig/schemable/definition.rbs b/sig/schemable/definition.rbs index 54a0002..0780946 100644 --- a/sig/schemable/definition.rbs +++ b/sig/schemable/definition.rbs @@ -247,5 +247,15 @@ module Schemable # 'users' for the User model # 'citizen_applications' for the CitizenApplication model def model_name: -> String + + # Given a hash, it returns a new hash with all the keys camelized. + # + # @param hash [Array | Hash] The hash with all the keys camelized. + # + # @return [Array | Hash] The hash with all the keys camelized. + # + # @example + # { first_name: 'John', last_name: 'Doe' } => { firstName: 'John', lastName: 'Doe' } + def camelize_keys: (Hash[Symbol, any]) -> (Hash[Symbol, any] | Array[Hash[Symbol, any]]) end end