Sha256: 3f9f2d0b3fd15275a20c558c95e9e4cb21d31e7d83cb67f2b6811aeba2351f82

Contents?: true

Size: 1.79 KB

Versions: 44

Compression:

Stored size: 1.79 KB

Contents

# Custom Camelizer with CloudFormation specific handling.
# Based on: https://stackoverflow.com/questions/8706930/converting-nested-hash-keys-from-camelcase-to-snake-case-in-ruby
module Jets
  class Camelizer
    class << self
      def transform(value, parent_keys=[])
        case value
        when Array
          value.map { |v| transform(v) }
        when Hash
          initializer = value.map do |k, v|
            new_key = camelize_key(k, parent_keys)
            [new_key, transform(v, parent_keys+[new_key])]
          end
          Hash[initializer]
        else
          value # do not transform values
         end
      end

      def camelize_key(k, parent_keys=[])
        k = k.to_s

        if passthrough?(k, parent_keys)
          k # pass through untouch
        elsif parent_keys.last == "EventPattern" # top-level
          k.dasherize
        elsif parent_keys.include?("EventPattern")
          # Any keys at 2nd level under EventPattern will be pascalized
          pascalize(k)
        else
          camelize(k)
        end
      end

      def passthrough?(k, parent_keys)
        parent_keys.include?("Variables") || # do not transform keys anything under Variables
        parent_keys.include?("ResponseParameters") || # do not transform keys anything under Variables
        k.include?('-') || k.include?('/')
      end

      def camelize(value)
        return value if value.is_a?(Integer)

        value = value.to_s.camelize
        special_map[value] || value
      end

      def pascalize(value)
        new_value = value.camelize
        first_char = new_value[0..0].downcase
        new_value[0] = first_char
        new_value
      end

      # Some keys have special mappings
      def special_map
        {
          "TemplateUrl" => "TemplateURL"
        }
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
jets-1.1.5 lib/jets/camelizer.rb
jets-1.1.4 lib/jets/camelizer.rb
jets-1.1.3 lib/jets/camelizer.rb
jets-1.1.2 lib/jets/camelizer.rb
jets-1.1.1 lib/jets/camelizer.rb
jets-1.1.0 lib/jets/camelizer.rb
jets-1.0.18 lib/jets/camelizer.rb
jets-1.0.17 lib/jets/camelizer.rb
jets-1.0.16 lib/jets/camelizer.rb
jets-1.0.15 lib/jets/camelizer.rb
jets-1.0.13 lib/jets/camelizer.rb
jets-1.0.12 lib/jets/camelizer.rb
jets-1.0.11 lib/jets/camelizer.rb
jets-1.0.10 lib/jets/camelizer.rb
jets-1.0.9 lib/jets/camelizer.rb
jets-1.0.8 lib/jets/camelizer.rb
jets-1.0.7 lib/jets/camelizer.rb
jets-1.0.6 lib/jets/camelizer.rb
jets-1.0.5 lib/jets/camelizer.rb
jets-1.0.4 lib/jets/camelizer.rb