Class: Jets::Camelizer
- Inherits:
-
Object
- Object
- Jets::Camelizer
- Defined in:
- lib/jets/camelizer.rb
Class Method Summary collapse
- .camelize(value) ⇒ Object
- .camelize_key(k, parent_keys = []) ⇒ Object
- .pascalize(value) ⇒ Object
- .passthrough?(k, parent_keys) ⇒ Boolean
-
.special_map ⇒ Object
Some keys have special mappings.
- .transform(value, parent_keys = []) ⇒ Object
Class Method Details
.camelize(value) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/jets/camelizer.rb', line 42 def camelize(value) return value if value.is_a?(Integer) value = value.to_s.camelize special_map[value] || value end |
.camelize_key(k, parent_keys = []) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jets/camelizer.rb', line 21 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 |
.pascalize(value) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/jets/camelizer.rb', line 49 def pascalize(value) new_value = value.camelize first_char = new_value[0..0].downcase new_value[0] = first_char new_value end |
.passthrough?(k, parent_keys) ⇒ Boolean
36 37 38 39 40 |
# File 'lib/jets/camelizer.rb', line 36 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 |
.special_map ⇒ Object
Some keys have special mappings
57 58 59 60 61 62 63 64 |
# File 'lib/jets/camelizer.rb', line 57 def special_map { "TemplateUrl" => "TemplateURL", "Ttl" => "TTL", "MaxReceiveCount" => "maxReceiveCount", "DeadLetterTargetArn" => "deadLetterTargetArn", } end |
.transform(value, parent_keys = []) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/jets/camelizer.rb', line 6 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 |