Class: MPS::Config
- Inherits:
-
Object
- Object
- MPS::Config
- 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
-
#git_branch ⇒ Object
readonly
Returns the value of attribute git_branch.
-
#git_remote ⇒ Object
readonly
Returns the value of attribute git_remote.
-
#log_file ⇒ Object
readonly
Returns the value of attribute log_file.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#storage_dir ⇒ Object
readonly
Returns the value of attribute storage_dir.
Class Method Summary collapse
-
.init(conf_fp) ⇒ Conf
initialize a yaml config file for mps if the file already exists skips.
-
.load_conf_hash(conf_fp) ⇒ Hash
Loads a yaml configuration file.
Instance Method Summary collapse
-
#initialize(**conf_hash) ⇒ Config
constructor
A new instance of Config.
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_branch ⇒ Object (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_remote ⇒ Object (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_file ⇒ Object (readonly)
Returns the value of attribute log_file.
17 18 19 |
# File 'lib/mps/config.rb', line 17 def log_file @log_file end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
16 17 18 |
# File 'lib/mps/config.rb', line 16 def logger @logger end |
#storage_dir ⇒ Object (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
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
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
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 |