Class: HashWithQuickAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_with_quick_access.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashWithQuickAccess

Returns a new instance of HashWithQuickAccess.



2
3
4
# File 'lib/hash_with_quick_access.rb', line 2

def initialize(hash)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/hash_with_quick_access.rb', line 6

def method_missing(key)
  if key?(key)
    fetch_possibly_decorated_value(key)
  else
    fail KeyError, "key :#{key} was not found"
  end
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/hash_with_quick_access.rb', line 22

def [](key)
  send(key)
end

#keysObject



18
19
20
# File 'lib/hash_with_quick_access.rb', line 18

def keys
  @hash.keys.map(&:to_sym)
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/hash_with_quick_access.rb', line 14

def respond_to?(method_name, include_private = false)
  key?(method_name) || super
end