Class: Hoth::Transport::Bert::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/hoth/transport/bert.rb

Class Method Summary collapse

Class Method Details

.deserialize(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hoth/transport/bert.rb', line 34

def self.deserialize(data)
  case data
  when BERT::Tuple
    tuple2ruby data
  when Array
    data.collect { |o| deserialize o }
  when Hash
    data.each { |k,v| data[k] = deserialize(v) }
  else
    data
  end
end

.tuple2ruby(tuple) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hoth/transport/bert.rb', line 47

def self.tuple2ruby(tuple)
  case tuple
  when BERT::Tuple
    begin
      ruby_class = tuple.first.camelize.constantize
      ruby_obj = ruby_class.new({})
      ruby_obj.to_serialize.each do |field|
        ruby_obj.send("#{field}=", deserialize(tuple.last[field]))
      end
      
      ruby_obj
    rescue NameError => e
      puts %Q{An Exception occured: #{e.message} -- #{e.backtrace.join("\n\t")}}
      tuple
    end
  else
    puts "Was not anything we could decode!"
    tuple
  end
end