Class: MPS::Config

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

Overview

Configuration class

To use an instance of the class to contain necessary configuration data and tasks

Defined Under Namespace

Classes: ConfigFileAlreadyExist, ConfigFileNotFound, LoadError, MPSDirectoryNotFound, MPSStorageDirectoryNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**conf_hash) ⇒ Config

Returns a new instance of Config.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mps/config.rb', line 20

def initialize(**conf_hash)
  @mps_dir = conf_hash[:mps_dir]
  @storage_dir = conf_hash[:storage_dir]
  @log_file = conf_hash[:log_file]
  @git_remote = conf_hash.fetch(:git_remote, "origin")
  @git_branch = conf_hash.fetch(:git_branch, "master")
  @logger = Logger.new(File.open(@log_file, "a+"))
  @logger.formatter = proc do |sev, time, pn, msg|
    time = time.strftime("[%Y-%m-%d %H:%M:%S]")
    sev = {"INFO"=> "I", "WARN"=>"W", "ERROR"=>"E", "FATAL"=> "F", "DEBUG"=>"D"}[sev]
    "#{time} #{sev}: #{msg}"
  end
end

Instance Attribute Details

#git_branchObject (readonly)

Returns the value of attribute git_branch.



19
20
21
# File 'lib/mps/config.rb', line 19

def git_branch
  @git_branch
end

#git_remoteObject (readonly)

Returns the value of attribute git_remote.



18
19
20
# File 'lib/mps/config.rb', line 18

def git_remote
  @git_remote
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



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

def log_file
  @log_file
end

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/mps/config.rb', line 16

def logger
  @logger
end

#storage_dirObject (readonly)

Returns the value of attribute storage_dir.



15
16
17
# File 'lib/mps/config.rb', line 15

def storage_dir
  @storage_dir
end

Class Method Details

.init(conf_fp) ⇒ Conf

initialize a yaml config file for mps if the file already exists skips

Parameters:

  • conf_fp (String)

    file path to the yaml config file

  • force (Boolean)

    forces even file exists

Returns:

  • (Conf)

    Conf object



58
59
60
61
62
# File 'lib/mps/config.rb', line 58

def self.init(conf_fp)
  File.open(conf_fp, "w+") do |f|
    YAML.dump(Constants::DEFAULT_CONF_HASH, f)
  end
end

.load_conf_hash(conf_fp) ⇒ Hash

Note:

This is method checks for the loaded yaml data, if it doesn’t contain necessary data conforming to the contextual configuration data, should raise appropriate StandardError.

Loads a yaml configuration file

Parameters:

  • conf_fp (String)

    file path to the yaml config file

Returns:

  • (Hash)

    should return a Config hash

Raises:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mps/config.rb', line 40

def self.load_conf_hash(conf_fp)
  if File.exist?(conf_fp)
    conf_hash = YAML.load_file(conf_fp)
    raise LoadError.new("yaml not a hash") if conf_hash.class!=Hash
    raise LoadError.new("storage_dir key requires") if not conf_hash[:storage_dir]
    raise LoadError.new("log_file key requires") if not conf_hash[:log_file]
    raise LoadError.new("mps_dir key requires") if not conf_hash[:mps_dir]
    return conf_hash
  end
  raise ConfigFileNotFound.new("configs file not found")
end