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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mps/config.rb', line 23

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")
  @default_command = conf_hash.fetch(:default_command, "open")
  @type_aliases    = conf_hash.fetch(:aliases, {})
  @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

#default_commandObject (readonly)

Returns the value of attribute default_command.



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

def default_command
  @default_command
end

#git_branchObject (readonly)

Returns the value of attribute git_branch.



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

def git_branch
  @git_branch
end

#git_remoteObject (readonly)

Returns the value of attribute git_remote.



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

def git_remote
  @git_remote
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



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

def log_file
  @log_file
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#mps_dirObject (readonly)

Returns the value of attribute mps_dir.



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

def mps_dir
  @mps_dir
end

#storage_dirObject (readonly)

Returns the value of attribute storage_dir.



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

def storage_dir
  @storage_dir
end

#type_aliasesObject (readonly)

Returns the value of attribute type_aliases.



22
23
24
# File 'lib/mps/config.rb', line 22

def type_aliases
  @type_aliases
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



63
64
65
66
67
# File 'lib/mps/config.rb', line 63

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:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mps/config.rb', line 45

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