Module: Hashie::Extensions::MethodWriter

Defined in:
lib/hashie/extensions/method_access.rb

Overview

MethodWriter gives you #key_name= shortcuts for writing to your hash. Keys are written as strings, override #convert_key if you would like to have symbols or something else.

Note that MethodWriter also overrides #respond_to such that any #method_name= will respond appropriately as true.

Examples:

class MyHash < Hash
  include Hashie::Extensions::MethodWriter
end

h = MyHash.new
h.awesome = 'sauce'
h['awesome'] # => 'sauce'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/hashie/extensions/method_access.rb', line 75

def method_missing(name, *args)
  if args.size == 1 && name.to_s =~ /(.*)=$/
    return self[convert_key(Regexp.last_match[1])] = args.first
  end

  super
end

Instance Method Details

#convert_key(key) ⇒ Object



83
84
85
# File 'lib/hashie/extensions/method_access.rb', line 83

def convert_key(key)
  key.to_s
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/hashie/extensions/method_access.rb', line 70

def respond_to_missing?(name, include_private = false)
  return true if name.to_s =~ /=$/
  super
end