Class: ExStruct

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

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ ExStruct

Returns a new instance of ExStruct.



3
4
5
6
# File 'lib/ex_struct.rb', line 3

def initialize(hash=nil)
  @table = {}
  regist(hash) if hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ex_struct.rb', line 16

def method_missing(mid, *args)
  mname = mid.id2name
  len = args.length
  if mname.chomp!('=')
    if len != 1
      raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
    end
    modifiable[new_ostruct_member(mname)] = args[0]
  elsif mname.chomp!('?')
    if len != 0
      raise ArgumentError, "wrong number of arguments (#{len} for 0)", caller(1)
    end
    @table[mname.to_sym]
  elsif len == 0
    ret = @table[mid]
    ret = (modifiable[new_ostruct_member(mname)] = self.class.new) unless ret
    ret
  else  
    raise ArgumentError, "wrong number of arguments (#{len} for 0)", caller(1)
  end   
end