Module: RepoManager::Extensions::MethodWriter

Included in:
Settings
Defined in:
lib/repo_manager/extensions/hash.rb

Overview

MethodWriter gives you #key_name= shortcuts for writing to your hash. Keys are written as symbols

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

Examples:

Extending the Hash class


class MyHash < Hash
  include RepoManager::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



71
72
73
74
75
76
77
# File 'lib/repo_manager/extensions/hash.rb', line 71

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

  super
end

Instance Method Details

#convert_key(key) ⇒ Object



79
80
81
82
# File 'lib/repo_manager/extensions/hash.rb', line 79

def convert_key(key)
  # mod to return symbol keys instead of string keys
  key.to_sym
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/repo_manager/extensions/hash.rb', line 66

def respond_to?(name)
  return true if name.to_s =~ /=$/
  super
end