Sha256: 9f793dfa7cbcf05e23cb29d806964322bcbd3165afa2a09cd8099ab9c63166c7

Contents?: true

Size: 911 Bytes

Versions: 16

Compression:

Stored size: 911 Bytes

Contents

class Pascalize
  class << self
    # Specialized pascalize that will not pascalize keys under the
    # Variables part of the hash structure.
    # Based on: https://stackoverflow.com/questions/8706930/converting-nested-hash-keys-from-camelcase-to-snake-case-in-ruby
    def pascalize(value, parent_key=nil)
      case value
        when Array
          value.map { |v| pascalize(v) }
        when Hash
          initializer = value.map do |k, v|
            new_key = pascal_key(k, parent_key)
            [new_key, pascalize(v, new_key)]
          end
          Hash[initializer]
        else
          value
       end
    end

    def pascal_key(k, parent_key=nil)
      if parent_key == "Variables" # do not pascalize keys anything under Variables
        k
      else
        k = k.to_s.camelize
        k.slice(0,1).capitalize + k.slice(1..-1) # capitalize first letter only
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
jets-0.6.6 lib/jets/pascalize.rb
jets-0.6.5 lib/jets/pascalize.rb
jets-0.6.4 lib/jets/pascalize.rb
jets-0.6.3 lib/jets/pascalize.rb
jets-0.6.2 lib/jets/pascalize.rb
jets-0.6.1 lib/jets/pascalize.rb
jets-0.6.0 lib/jets/pascalize.rb
jets-0.5.8 lib/jets/pascalize.rb
jets-0.5.7 lib/jets/pascalize.rb
jets-0.5.6 lib/jets/pascalize.rb
jets-0.5.5 lib/jets/pascalize.rb
jets-0.5.4 lib/jets/pascalize.rb
jets-0.5.3 lib/jets/pascalize.rb
jets-0.5.2 lib/jets/pascalize.rb
jets-0.5.1 lib/jets/pascalize.rb
jets-0.5.0 lib/jets/pascalize.rb