Module: OFC2::OWJSON

Included in:
Graph
Defined in:
lib/ofc2.rb

Overview

specjal module included in each class with that module we add to_hash method there is also a method_missing which allow user to set/get any instance variable if user try to get not setted instance variable it return nil and generate a warn

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments) ⇒ Object

method_missing handle setting and getting instance variables You can set variable in two ways:

  1. variable_name = value

  2. set_variable_name(value)

you can get only alredy setted variables, otherwise return nil



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ofc2.rb', line 27

def method_missing(method_id, *arguments)
  a = arguments[0] if arguments and arguments.size > 0
  method = method_id.to_s
  if method =~ /^(.*)(=)$/
    self.instance_variable_set("@#{$1.gsub('_','__')}", a)
  elsif method =~ /^(set_)(.*)$/
    self.instance_variable_set("@#{$2.gsub('_','__')}", a)
  elsif self.instance_variable_defined?("@#{method_id.to_s.gsub('_','__')}")
    self.instance_variable_get("@#{method_id.to_s.gsub('_','__')}") # that will be return instance variable value or nil, handy
  else
    nil
  end
end

Instance Method Details

#to_hashObject Also known as: to_h

return a hash of instance values



9
10
11
# File 'lib/ofc2.rb', line 9

def to_hash
  self.instance_values
end