Module: Spectate::Config

Defined in:
lib/spectate/config.rb

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



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

def self.[](key)
  @config[key]
end

.[]=(key, val) ⇒ Object



17
18
19
# File 'lib/spectate/config.rb', line 17

def self.[]=(key, val)
  @config[key] = val
end

.basedirObject



34
35
36
# File 'lib/spectate/config.rb', line 34

def self.basedir
  @basedir
end

.clear!Object



25
26
27
28
# File 'lib/spectate/config.rb', line 25

def self.clear!
  @config = Hash.new
  @loaded = false
end

.default(hash) ⇒ Object



30
31
32
# File 'lib/spectate/config.rb', line 30

def self.default(hash)
  @config = hash.merge(@config)
end

.delete(key) ⇒ Object



21
22
23
# File 'lib/spectate/config.rb', line 21

def self.delete(key)
  @config.delete(key)
end

.generate_configurationObject

Confirms that the config.yml file doesn’t already exist, then creates one from passed parameters



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spectate/config.rb', line 54

def self.generate_configuration
  configfile = File.join(self['basedir'], "config.yml")
  raise "You already have a config.yml file!  We don't want to mess with a good thing.\n" +
        "If you really want to start over, delete #{configfile} and run spectate --setup again." if File.exists?(configfile)
  unless File.directory?(self['basedir'])
    puts "Creating directory #{self['basedir']}"
    FileUtils.makedirs self['basedir']
  end
  puts "Creating config.yml file"
  File.open(configfile, 'w') {|config| YAML.dump(self.to_hash, config)}
  puts "Creating rackup file"
  FileUtils.copy File.join(File.dirname(__FILE__), '..', '..', 'generators', 'config.ru'), self['basedir']
end

.load_configuration(confdir = nil) ⇒ Object

Loads persistent values from the config.yml file in the base directory. Assumes that the basedir configuration variable has already been set before calling.



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

def self.load_configuration(confdir = nil)
  @basedir = confdir || self['basedir'] || ENV['SPECTATE_DIR'] || ROOT_DIR
  raise "Could not find a base directory!\nRun spectate --setup to initialize things or tell us your base directory\nwith the -d option." unless basedir
  raise "Directory #{basedir} not found!\nRun spectate --setup to initialize things." unless File.directory?(basedir)
  conffile = File.join(basedir, "config.yml")
  raise "File #{conffile} not found!\nRun spectate --setup to initialize things." unless File.exists?(conffile)
  @loaded = @config.merge!(YAML.load_file(conffile))
end

.loaded?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/spectate/config.rb', line 38

def self.loaded?
  @loaded
end

.to_hashObject



9
10
11
# File 'lib/spectate/config.rb', line 9

def self.to_hash
  @config
end