module Hashie::Extensions::Mash::TransformKeys

Applies a class-configured normalization to string and symbol Mash keys.

@example

klass = Class.new(Hashie::Mash) do
  include Hashie::Extensions::Mash::TransformKeys
  transform_keys_with(&:downcase)
end
klass.new('NAME' => 'Ada').name # => 'Ada'

Public Class Methods

included(base) click to toggle source

@example Install class-level transformation configuration

klass = Class.new(Hashie::Mash) do
  include Hashie::Extensions::Mash::TransformKeys
end
klass.key_transformer # => nil
# File lib/hashie/extensions/mash/transform_keys.rb, line 20
def self.included(base)
  raise ArgumentError, "`#{base}` must descend from Hashie::Mash" unless base <= Hashie::Mash

  base.extend(ClassMethods)
end

Private Instance Methods

convert_key(key) click to toggle source
Calls superclass method
# File lib/hashie/extensions/mash/transform_keys.rb, line 74
def convert_key(key)
  transformer = self.class.key_transformer
  return super unless transformer && (key.is_a?(String) || key.is_a?(Symbol))

  transformed = transformer.call(key.to_s)
  raise TypeError, 'key transformer must return a String' unless transformed.is_a?(String)

  super(transformed)
end