Class: Knj::Hash_methods
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
-
#args ⇒ Object
Returns the args-key.
-
#data ⇒ Object
Returns the data-key.
-
#db ⇒ Object
Returns the db-key.
-
#initialize(hash = {}) ⇒ Hash_methods
constructor
Spawns the object and takes a hash as argument.
-
#method_missing(method, *args) ⇒ Object
Proxies methods into the hash as keys.
-
#ob ⇒ Object
Returns the ob-key.
- #to_hash ⇒ Object
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
#args ⇒ Object
Returns the args-key.
22 23 24 |
# File 'lib/knj/hash_methods.rb', line 22 def args return self[:args] end |
#data ⇒ Object
Returns the data-key.
27 28 29 |
# File 'lib/knj/hash_methods.rb', line 27 def data return self[:data] end |
#db ⇒ Object
Returns the db-key.
12 13 14 |
# File 'lib/knj/hash_methods.rb', line 12 def db return self[:db] end |
#ob ⇒ Object
Returns the ob-key.
17 18 19 |
# File 'lib/knj/hash_methods.rb', line 17 def ob return self[:ob] end |
#to_hash ⇒ Object
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 |