Module: HashAccess::InstanceMethods
- Defined in:
- lib/hash_access.rb
Overview
This module contains only one method - instance method access_by_methods, which is being added to class Hash after inclusion of HashAccess module in any context (any module or class). All other methods are being generated by access_by_methods. Generated methods becomes instance methods, not the class methods as RDoc thinks :)
Below, the list of generated methods:
-
store, []=
-
method_missing
-
stringify_keys!
Instance Method Summary collapse
-
#access_by_methods ⇒ Object
Generates singleton instance methods, which allows access to hash elements with methods.
Instance Method Details
#access_by_methods ⇒ Object
Generates singleton instance methods, which allows access to hash elements with methods.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/hash_access.rb', line 22 def access_by_methods class << self alias_method :__hash_store, :store alias_method :__hash_assign, :[]= # Generated instance method. Assigns the value to key. def store(key, value) key = key.to_s __hash_store(key, value) if value.is_a? Hash value.access_by_methods end end # def store # Generated instance method. Assigns the value to key. def []=(key, value) self.store(key, value) end # def []= # Generated instance method. # Creates new hash element with the method key. def method_missing(method, *values) method_name = method.to_s super(method, *values) unless method_name =~ /^[a-z0-9]+.*$/ if method_name =~ /^.*=$/ key = method_name.chop if values.size == 1 self[key] = values[0] else self[key] = values end else self[method_name] = Hash.new unless self.keys.include?(method_name) self[method_name] end end # def method_missing # Generated instance method. Converts all hash keys to strings. def stringify_keys! keys.each do |key| next if key.is_a? String self[key.to_s] = self[key] delete(key) end self end # def stringify_keys! end # class self.stringify_keys! self.each do |key, value| if value.is_a? Hash value.access_by_methods end end end |