Module: Aliyun::OSS::Parsing::Typecasting

Included in:
XmlParser
Defined in:
lib/aliyun/oss/parsing.rb

Instance Method Summary collapse

Instance Method Details

#typecast(object) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/aliyun/oss/parsing.rb', line 17

def typecast(object)
  case object
  when Hash
    typecast_hash(object)
  when Array
    object.map {|element| typecast(element)}
  when String
    CoercibleString.coerce(object)
  else
    object
  end
end

#typecast_hash(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aliyun/oss/parsing.rb', line 30

def typecast_hash(hash)
  if content = hash['__content__']  
    typecast(content)
  else
    keys   = hash.keys.map {|key| key.underscore}
    values = hash.values.map {|value| typecast(value)}
    keys.inject({}) do |new_hash, key|
      new_hash[key] = values.slice!(0)
      new_hash
    end
  end
end