module Hashie::Extensions::Mash::PermissiveRespondTo

Allow a Mash to properly respond to everything

By default, Mashes only say they respond to methods for keys that exist in their key set or any of the affix methods (e.g. setter, underbang, etc.). This causes issues when you try to use them within a SimpleDelegator or bind to a method for a key that is unset.

This extension allows a Mash to properly respond to ‘respond_to?` and `method` for keys that have not yet been set. This enables full compatibility with SimpleDelegator and thunk-oriented programming.

There is a trade-off with this extension: it will run slower than a regular Mash; insertions and initializations with keys run approximately 20% slower and cost approximately 19KB of memory per class that you make permissive.

@api public @example Make a new, permissively responding Mash subclass

class PermissiveMash < Hashie::Mash
  include Hashie::Extensions::Mash::PermissiveRespondTo
end

mash = PermissiveMash.new(a: 1)
mash.respond_to? :b  #=> true