Module: RepoManager::Extensions::MethodReader

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

Overview

MethodReader allows you to access keys of the hash via method calls. This gives you an OStruct like way to access your hash’s keys. It will recognize keys either as strings or symbols.

Examples:

Extending the Hash class


class User < Hash
  include RepoManager::Extensions::MethodReader
end

user = User.new
user['first_name'] = 'Michael'
user.first_name # => 'Michael'

user[:last_name] = 'Bleigh'
user.last_name # => 'Bleigh'

user[:birthday] = nil
user.birthday # => nil

user.not_declared # => nil

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/repo_manager/extensions/hash.rb', line 38

def method_missing(name, *args)
  return self[name.to_s] if key?(name.to_s)
  return self[name.to_sym] if key?(name.to_sym)

  # mod to return nil instead of 'undefined method'
  return nil if args.length == 0

  super
end

Instance Method Details

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/repo_manager/extensions/hash.rb', line 33

def respond_to?(name)
  return true if key?(name.to_s) || key?(name.to_sym)
  super
end