Module: Oolite

Defined in:
lib/oolite.rb,
lib/oolite/trade.rb,
lib/oolite/market.rb,
lib/oolite/csv_doc.rb,
lib/oolite/version.rb,
lib/oolite/save_file.rb,
lib/oolite/market_file.rb

Defined Under Namespace

Classes: CSVDoc, Configuration, Market, MarketFile, SaveFile, Trade

Constant Summary collapse

CONFIG_FILE_NAME =
'.oolite'
VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



13
14
15
# File 'lib/oolite.rb', line 13

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Setup oolite configuration

Attempts to find and load a configuration file the first time it’s requested. If a config file cannot be found in the current directory tree (moving towards trunk, not the leaves), the user’s home directory will be searched. If still not found, a default configuration object is created.

If a block is provided, the configuration object is yielded to the block after the configuration is loaded/created.

Yields:



29
30
31
32
33
34
35
36
# File 'lib/oolite.rb', line 29

def self.configure
  if self.configuration.nil?
    unless self.load_configuration
      self.configuration = Configuration.new
    end
  end
  yield(configuration) if block_given?
end

.find_config_pathObject

Walk up the directory tree from current working dir (pwd) till a file named .oolite is found

Returns file path if found, nil if not.



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

def self.find_config_path
  path = Pathname(Pathname.pwd).ascend{|d| h=d+CONFIG_FILE_NAME; break h if h.file?}
  if path.nil? && (Pathname(ENV['HOME']) + CONFIG_FILE_NAME).exist?
    path = Pathname(ENV['HOME']) + CONFIG_FILE_NAME
  end
  path
end

.load_configuration(path = nil) ⇒ Object

Load the configuration from disk

Returns true if config file found and loaded, false otherwise.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oolite.rb', line 88

def self.load_configuration(path = nil)
  # If no path provided, see if we can find one in the dir tree.
  if path.nil?
    path = find_config_path
  end

  return false if path.nil?
  return false unless Pathname(path).exist?

  File.open(path, 'r') do |f|
    self.configuration = YAML.load(f)
    puts "configuration loaded from #{path}" if $debug
  end

  true
end

.save_configuration(path = nil) ⇒ Object

Write configuration to disk

Writes to current working dir (pwd) if path is nil

Returns path of emitted file



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/oolite.rb', line 61

def self.save_configuration(path = nil)
  # If no path provided, see if we can find one in the dir tree.
  if path.nil?
    path = find_config_path
  end

  # Still no path? Use the current working dir.
  if path.nil?
    path = Pathname.pwd
  end

  unless path.to_s.end_with?('/' + CONFIG_FILE_NAME)
    path = Pathname(path) + CONFIG_FILE_NAME
  end

  path = Pathname(path).expand_path
  File.write(path, YAML.dump(configuration))

  path
end