class Oolite::SaveFile

Public Class Methods

new(filename) click to toggle source
# File lib/oolite/save_file.rb, line 15
def initialize filename
  parse filename
end

Public Instance Methods

cargo() click to toggle source
# File lib/oolite/save_file.rb, line 32
def cargo
  node = get_data_node_for_key 'max_cargo'
  node.text.to_i
end
credits() click to toggle source
# File lib/oolite/save_file.rb, line 37
def credits
  node = get_data_node_for_key 'credits'
  node.text.to_i
end
current_system_name() click to toggle source
# File lib/oolite/save_file.rb, line 27
def current_system_name
  node = get_data_node_for_key 'current_system_name'
  node.text
end
local_market_data() click to toggle source
# File lib/oolite/save_file.rb, line 42
def local_market_data
  node = get_data_node_for_key 'localMarket'

  mdata = {}

  node.children.each do |child|
    type = child.children[0].text
    amount = child.children[1].text.to_i
    # Price is stored as an integer and displayed as price / 10.
    price = child.children[2].text.to_i
    mdata[type] = { amount: amount, price: price }
  end

  mdata
end
parse(filename) click to toggle source
# File lib/oolite/save_file.rb, line 19
def parse filename
  raise "Cannot find save file #{filename}" unless Pathname(filename).exist?

  @doc = Nokogiri::XML(File.open(filename)) do |config|
    config.noblanks
  end
end

Private Instance Methods

get_data_node_for_key(key) click to toggle source
# File lib/oolite/save_file.rb, line 60
def get_data_node_for_key key
  @doc.css('dict').children.each do |node|
    if node.name == 'key' && node.text == key
      return node.next
    end
  end

  raise "No data found for key #{key}"
end