Class: Enc::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/enc/config.rb

Defined Under Namespace

Classes: ConfigurationDoesNotExist, InvalidConfiguration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil, options = {}, use_file = true) ⇒ Config

Returns a new instance of Config.



8
9
10
11
12
# File 'lib/enc/config.rb', line 8

def initialize(config_file=nil, options={}, use_file=true)
  @config_file = get_default_config(config_file)
  @options = options.each { |k,v| {k.to_sym => v} }
  @use_file = use_file
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



3
4
5
# File 'lib/enc/config.rb', line 3

def config_file
  @config_file
end

Instance Method Details

#config_file_existsObject



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

def config_file_exists
  File.exist?(@config_file)
end

#create!Object



30
31
32
33
34
# File 'lib/enc/config.rb', line 30

def create!
  raise InvalidConfiguration, 'Configuration requires host, username and password to be present.' unless
    @options[:host] and @options[:username] and @options[:password]
  File.open(@config_file, 'w') {|f| f.write @options.to_yaml }
end

#delete!Object



36
37
38
# File 'lib/enc/config.rb', line 36

def delete!
  File.delete(@config_file) if File.exist?(@config_file)
end

#get(key) ⇒ Object



14
15
16
# File 'lib/enc/config.rb', line 14

def get(key)
  load[key.to_sym]
end

#loadObject



18
19
20
21
22
23
24
# File 'lib/enc/config.rb', line 18

def load
  if @options.empty?
    @options = YAML.load_file(@config_file).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
  end
  set_defaults
  @options
end