Class: Knj::Hash_methods

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

Overview

A normal hash that uses ‘method_missing’ to be able to call keys by using methods. It is heavily used by Knj::Objects and have some pre-defined methods because of it to optimize performance.

Examples

hm = Knj::Hash_methods(:test => “Test”) print hm.test

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Hash_methods

Spawns the object and takes a hash as argument.



7
8
9
# File 'lib/knj/hash_methods.rb', line 7

def initialize(hash = {})
  self.update(hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Proxies methods into the hash as keys.



41
42
43
44
45
46
# File 'lib/knj/hash_methods.rb', line 41

def method_missing(method, *args)
  method = method.to_sym
  return self[method] if self.key?(method)
  
  raise "No such method '#{method}' on class '#{self.class.name}'"
end

Instance Method Details

#argsObject

Returns the args-key.



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

def args
  return self[:args]
end

#dataObject

Returns the data-key.



27
28
29
# File 'lib/knj/hash_methods.rb', line 27

def data
  return self[:data]
end

#dbObject

Returns the db-key.



12
13
14
# File 'lib/knj/hash_methods.rb', line 12

def db
  return self[:db]
end

#obObject

Returns the ob-key.



17
18
19
# File 'lib/knj/hash_methods.rb', line 17

def ob
  return self[:ob]
end

#to_hashObject



31
32
33
34
35
36
37
38
# File 'lib/knj/hash_methods.rb', line 31

def to_hash
  h = {}
  self.each do |key, val|
    h[k] = val
  end
  
  return h
end