Module: Evesync::IPC::Data::Unhashable

Included in:
File, Ignore, Package
Defined in:
lib/evesync/ipc/data/hashable.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(hash) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/evesync/ipc/data/hashable.rb', line 33

def from_hash(hash)
  Log.debug("IPC Data message hash parsing: #{hash}")
  params = {}
  hash.each do |key, value|
    next unless key =~ /^@/

    if value.is_a? Hash
      # FIXME: code dumplication ipc_data.rb:31
      begin
        cl = Object.const_get value['type']
      rescue NameError => e
        Log.fatal("IPC Data Unsupported type: #{hash['type']}")
        raise e
      end

      unless cl.respond_to? :from_hash
        err_msg = "IPC Data ERROR Class #{cl} must implement `self.from_hash'"
        Log.fatal(err_msg)
        raise err_msg
      end

      complex_value = cl.from_hash value
      params[key.sub('@', '').to_sym] = complex_value
    else
      params[key.sub('@', '').to_sym] = value
    end
  end

  begin
    # If the `type' is imported it will be used
    cl = Object.const_get hash['type']
  rescue TypeError, NameError
    # Or the base class will be the type
    cl = self
  end

  cl.new(params)
end