module Plissken::Methods
Public Instance Methods
to_snake_keys(value = self)
click to toggle source
Recursively converts CamelCase and camelBack JSON-style hash keys to Rubyish snake_case, suitable for use during instantiation of Ruby model attributes.
# File lib/plissken/methods.rb, line 9 def to_snake_keys(value = self) case value when Array value.map { |v| to_snake_keys(v) } when Hash snake_hash(value) else value end end
Private Instance Methods
snake_hash(value)
click to toggle source
# File lib/plissken/methods.rb, line 22 def snake_hash(value) value.to_h { |k, v| [underscore_key(k), to_snake_keys(v)] } end
underscore(string)
click to toggle source
# File lib/plissken/methods.rb, line 37 def underscore(string) @__memoize_underscore ||= {} return @__memoize_underscore[string] if @__memoize_underscore[string] @__memoize_underscore[string] = string.tr("::", "/") .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr("-", "_") .downcase @__memoize_underscore[string] end
underscore_key(key)
click to toggle source
# File lib/plissken/methods.rb, line 26 def underscore_key(key) case key when Symbol underscore(key.to_s).to_sym when String underscore(key) else key # Plissken can't snakify anything except strings and symbols end end