Sha256: 5f9ec96b40c2a66806f84ebc9bb5ead02b6826abfcf21865f5de0e4010664755

Contents?: true

Size: 959 Bytes

Versions: 2

Compression:

Stored size: 959 Bytes

Contents

module Paysafe
  module Refinements
    module SnakeCase
      refine Hash do

        def to_snake_case(data = self)
          case data
          when Array
            data.map { |value| to_snake_case(value) }
          when Hash
            data.map { |key, value| [underscore_key(key), to_snake_case(value)] }.to_h
          else
            data
          end
        end

        private

        def underscore_key(key)
          underscore(key.to_s).to_sym
        end

        def underscore(string)
          @__memoize_underscore ||= {}
          return @__memoize_underscore[string] if @__memoize_underscore[string]
          @__memoize_underscore[string] =
            string.gsub(/::/, '/')
                  .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
                  .gsub(/([a-z\d])([A-Z])/, '\1_\2')
                  .tr('-', '_')
                  .downcase
          @__memoize_underscore[string]
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paysafe-0.16.0 lib/paysafe/refinements/snake_case.rb
paysafe-0.15.0 lib/paysafe/refinements/snake_case.rb