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-0.8.9 | lib/jets/camelizer.rb |
jets-0.8.8 | lib/jets/camelizer.rb |
jets-0.8.6 | lib/jets/camelizer.rb |
jets-0.8.5 | lib/jets/camelizer.rb |