Class: TestServer::Config
- Inherits:
-
Object
- Object
- TestServer::Config
- Defined in:
- lib/test_server/config.rb
Class Attribute Summary collapse
-
.options ⇒ Object
readonly
Returns the value of attribute options.
-
.process_environment ⇒ Object
readonly
Returns the value of attribute process_environment.
Class Method Summary collapse
- .option(option, default_value) ⇒ Object
- .option_reader(option, default_value) ⇒ Object
- .option_writer(option) ⇒ Object
Instance Method Summary collapse
- #allowed_config_file_paths ⇒ Object
-
#initialize(file = available_config_file, config_engine = Psych) ⇒ Config
constructor
A new instance of Config.
- #lock ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(file = available_config_file, config_engine = Psych) ⇒ Config
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/test_server/config.rb', line 46 def initialize(file = available_config_file, config_engine = Psych) config_mutex = Mutex.new config_mutex.synchronize do yaml = Psych.load_file(file) if yaml.respond_to? :[] @config = yaml.symbolize_keys else @config = {} end end rescue StandardError => e fail Exceptions::ConfigFileNotReadable, JSON.dump(message: e., file: file) end |
Class Attribute Details
.options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/test_server/config.rb', line 16 def end |
.process_environment ⇒ Object (readonly)
Returns the value of attribute process_environment.
16 17 18 |
# File 'lib/test_server/config.rb', line 16 def process_environment @process_environment end |
Class Method Details
.option(option, default_value) ⇒ Object
38 39 40 41 |
# File 'lib/test_server/config.rb', line 38 def option(option, default_value) option_reader(option, default_value) option_writer(option) end |
.option_reader(option, default_value) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/test_server/config.rb', line 18 def option_reader(option, default_value) define_method option.to_sym do config.fetch(option.to_sym, default_value) end << option end |
.option_writer(option) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/test_server/config.rb', line 26 def option_writer(option) define_method "#{option}=".to_sym do |value| begin config[option.to_sym] = value rescue RuntimeError raise Exceptions::ConfigLocked end end << option end |
Instance Method Details
#allowed_config_file_paths ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/test_server/config.rb', line 77 def allowed_config_file_paths [ ::File.(::File.join(self.class.process_environment.fetch('HOME'), '.config', 'test_server', 'config.yaml')), ::File.(::File.join(self.class.process_environment.fetch('HOME'), '.test_server', 'config.yaml')), ::File.(::File.join('/etc', 'test_server', 'config.yaml')), ::File.('../../../files/config.yaml', __FILE__), ] end |
#lock ⇒ Object
61 62 63 |
# File 'lib/test_server/config.rb', line 61 def lock config.freeze end |
#to_s ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/test_server/config.rb', line 65 def to_s result = [] result << sprintf("%20s | %s", 'option', 'value') result << sprintf("%s + %s", '-' * 20, '-' * 80) Config..each do |o| result << sprintf("%20s | %s", o, Array(public_send(o)).join(', ')) end result.join("\n") end |