Class: ROstruct

Inherits:
Object
  • Object
show all
Defined in:
lib/rostruct.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ROstruct

Returns a new instance of ROstruct.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rostruct.rb', line 4

def initialize(options = {})
  @as_hash = options.with_indifferent_access

  options.each do |key, value|
    if value.is_a?(Hash)
      value = ROstruct.new(value)
    end

    send("#{key}=", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rostruct.rb', line 32

def method_missing(method_name, *args, &block)
  if method_name.to_s.include?("=")
    method_name = method_name.to_s.gsub(/\=/) {}

    eigenclass.send :define_method, method_name do
      instance_variable_get("@#{method_name}")
    end

    eigenclass.send :define_method, "#{method_name}=" do |value|
      instance_variable_set("@#{method_name}", value)
    end

    send("#{method_name}=", *args, &block)
  else
    nil
  end
end

Instance Method Details

#to_hObject



16
17
18
# File 'lib/rostruct.rb', line 16

def to_h
  @as_hash
end