Class: Mir::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) {|Mir::Config| ... } ⇒ Mir::Config

Creates a new config instance

Parameters:

  • config_file (String) (defaults to: nil)

    path to the configuration file

Yields:



12
13
14
15
16
17
18
19
20
21
# File 'lib/mir/config.rb', line 12

def initialize(config_file = nil)
  @config_file = config_file
  @settings, @database = nil, nil
  
  self.max_upload_attempts = 10
  self.max_download_attempts = 10
  self.max_threads = 5
  
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/mir/config.rb', line 54

def method_missing(meth, *args, &block)
  val = @settings.send(meth)
  unless val.nil?
    val
  else
    raise UndefinedConfigValue
  end
end

Instance Attribute Details

#max_download_attemptsObject

Returns the value of attribute max_download_attempts.



23
24
25
# File 'lib/mir/config.rb', line 23

def max_download_attempts
  @max_download_attempts
end

#max_threadsObject

Returns the value of attribute max_threads.



23
24
25
# File 'lib/mir/config.rb', line 23

def max_threads
  @max_threads
end

#max_upload_attemptsObject

Returns the value of attribute max_upload_attempts.



23
24
25
# File 'lib/mir/config.rb', line 23

def max_upload_attempts
  @max_upload_attempts
end

Instance Method Details

#valid?Boolean

TODO:

move this into a class method responsible for parsing the YAML file. We shouldn’t have to explicitly check for a valid object unless the user has provided a settings file

Validates configuration settings

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mir/config.rb', line 29

def valid?
  if @config_file.nil? or !File.exist?(@config_file)
    Mir.logger.error("Configuration file not found")
    return false
  end
  
  if File.directory?(@config_file)
    Mir.logger.error("Received directory instead of settings file")
    return false
  end
  
  File.open(@config_file) do |f|
    yml = YAML::load(f)
    if yml.key? "settings"
      @settings = OpenStruct.new yml["settings"]
      @settings.database = symbolize_keys(@settings.database)
      @settings.cloud_provider = symbolize_keys(@settings.cloud_provider)
    else
      Mir.logger.error("Malformed config file")
      return false
    end
  end
  true
end