Class: Oolite::MarketFile

Inherits:
Object
  • Object
show all
Defined in:
lib/oolite/market_file.rb

Instance Method Summary collapse

Constructor Details

#initializeMarketFile

Returns a new instance of MarketFile.



15
16
17
18
19
20
21
# File 'lib/oolite/market_file.rb', line 15

def initialize
  Oolite.configure do |config|
    @save_file_path = Pathname(config.save_file_path)
    path = @save_file_path.dirname
    @market_data_path = path + (Pathname(config.market_data_filename).to_s + '.yml')
  end
end

Instance Method Details

#dataObject



23
24
25
# File 'lib/oolite/market_file.rb', line 23

def data
  @data ||= self.load
end

#data=(new_data) ⇒ Object



27
28
29
# File 'lib/oolite/market_file.rb', line 27

def data= new_data
  @data = new_data
end

#loadObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/oolite/market_file.rb', line 35

def load
  if @market_data_path.nil? || @market_data_path.to_s.empty? || !@market_data_path.exist?
    Hash.new
  else
    input = YAML.load_file(@market_data_path)
    input or Hash.new
  end
rescue
  Hash.new
end

#systemsObject



31
32
33
# File 'lib/oolite/market_file.rb', line 31

def systems
  data.keys
end

#writeObject



46
47
48
49
50
51
52
# File 'lib/oolite/market_file.rb', line 46

def write
  raise "Missing filename" if @market_data_path.nil? || @market_data_path.to_s.empty?

  File.open(@market_data_path, 'w') do |f|
    f.write data.to_yaml
  end
end