Sha256: 8f0f00fb1a78673ab66b19a7f3cc144df650904a052ebfab046aab3458510a51

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 KB

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)
          case key
          when Symbol
            underscore(key.to_s).to_sym
          when String
            underscore(key).to_sym
          else
            key
          end
        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

10 entries across 10 versions & 1 rubygems

Version Path
paysafe-0.14.0 lib/paysafe/refinements/snake_case.rb
paysafe-0.13.0 lib/paysafe/refinements/snake_case.rb
paysafe-0.12.0 lib/paysafe/refinements/snake_case.rb
paysafe-0.11.0 lib/paysafe/refinements/snake_case.rb
paysafe-0.10.0 lib/paysafe/refinements/snake_case.rb
paysafe-0.9.4 lib/paysafe/refinements/snake_case.rb
paysafe-0.9.3 lib/paysafe/refinements/snake_case.rb
paysafe-0.9.2 lib/paysafe/refinements/snake_case.rb
paysafe-0.9.1 lib/paysafe/refinements/snake_case.rb
paysafe-0.9.0 lib/paysafe/refinements/snake_case.rb