Class: OpenHash

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

Defined Under Namespace

Modules: VERSION

Instance Method Summary collapse

Methods inherited from Hash

#to_open_hash

Constructor Details

#initialize(hash = {}) ⇒ OpenHash

Returns a new instance of OpenHash.



5
6
7
8
9
# File 'lib/open_hash.rb', line 5

def initialize(hash = {})
  hash.each do |key, value|
    self[key] = value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/open_hash.rb', line 11

def method_missing(method_name, *args, &block)
  if method_name.to_s.end_with?("?")
    has_key?(method_name.to_s.chomp("?").to_sym) || has_key?(method_name.to_s.chomp("?"))
  elsif has_key?(method_name)
    self[method_name]
  elsif has_key?(method_name.to_s)
    self[method_name.to_s]
  elsif method_name.to_s.end_with?("=")
    self[method_name.to_s.chomp("=").to_sym] = args.first
  else
    super
  end
end

Instance Method Details

#methodsObject



25
26
27
# File 'lib/open_hash.rb', line 25

def methods
  super + setter_methods + getter_methods + predicate_methods
end