Module: MetaGraph::HashAccessor

Included in:
Node
Defined in:
lib/meta_graph/hash_accessor.rb

Overview

HashAccessor module provide a function that read value from hash as method. If this module is included on your class and specify a member variables symbol to hash_accessor class macro, you can get methods named hash’s key and call it, then they will return hash’s value.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

If the hash that specified to hash_accessor class macro has a key same as the called method name, get the hash value.



48
49
50
51
52
53
# File 'lib/meta_graph/hash_accessor.rb', line 48

def method_missing(method, *args, &block)
  value = read_hash(method) || read_hash(method.to_s)
  return value if value

  super(method, *args, &block)
end

Class Method Details

.included(klass) ⇒ Object

When this module is included on your class, it adds hash_accessor class macro to included class.



13
14
15
# File 'lib/meta_graph/hash_accessor.rb', line 13

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#read_hash(key) ⇒ Object

Read a hash value by specifing has key. Reading hash value In this method is the hash that specified in hash_accessor class macro.

Argument

key

specify hash key



37
38
39
40
41
42
# File 'lib/meta_graph/hash_accessor.rb', line 37

def read_hash(key)
  hash_name = self.class.instance_variable_get('@hash_name')
  hash = instance_variable_get("@#{hash_name}")

  hash[key] if hash.key?(key)
end