Class: XMLRPCStruct

Inherits:
Hash
  • Object
show all
Defined in:
lib/ec2/amitools/util.rb

Overview

Base class for XML-RPC structures. Stores key and values pairs. Key names are mapped to method names by converting ‘-’ to ‘_’ characters.

Instance Method Summary collapse

Constructor Details

#initialize(members) ⇒ XMLRPCStruct

members A list of the structure’s key names or nil if any key names are allowed.



73
74
75
76
77
78
# File 'lib/ec2/amitools/util.rb', line 73

def initialize members
  unless members.kind_of? Array or members.nil?
    raise ArgumentError.new( "invalid members argument" )
  end
  @members = members
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, argument = nil) ⇒ Object

Provide direct access to individual instance elements by methods named after the element’s key.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ec2/amitools/util.rb', line 82

def method_missing( method_symbol, argument=nil )
  # Here kid, play with this loaded gun...
  method = method_symbol.to_s
  
  # Determine if setter or getter call and remove '=' if setter.
  setter = /[\S]+=/.match(method)
  member = (setter ? method.slice(0, method.size - 1) : method)
  
  # Map method name to member name.
  member = member.gsub('_', '-')
 
  # If valid attribute set or get accordingly. If the member list is nil then
  # any members are allowed.
  if @members.nil? or @members.include?( member )
    if setter
      raise ArgumentError, "value for key #{member} may not be nil" if argument.nil?
      self[member] = argument
    else
      self[member]
    end
  else
    raise NoMethodError.new( method )
  end
end