Class: Topo::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/topo/loader.rb

Class Method Summary collapse

Class Method Details

.from_file(file, format = 'default') ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/topo/loader.rb', line 8

def self.from_file(file, format='default')
  
  unless File.file?(file)        
    STDERR.puts "ERROR: #{file} is not the name of a valid file."
    exit(-1)
  end
  
  begin
    data = JSON.parse(File.read(file))
    filename = File.basename(file)
    index = filename.rindex('.') || -1
    index -= 1 unless index == -1
    data['name'] = filename[0..index] unless data['name']
  rescue JSON::ParserError => e
    STDERR.puts e.message
    STDERR.puts "ERROR: Parsing error in #{file}."
    exit(-1)
  end
  
  Topo::Topology.new(Topo::Converter.convert(data, format))
end