Class: Jewelbox::Config
Overview
Description
Configuration object that represents ALL configuration items to be used by the current process.
Defined Under Namespace
Classes: Section
Class Method Summary collapse
-
.load(yml_path) ⇒ Object
Description Read the given YAML file, and add its content to Jewelbox.config.
-
.load_hash(h) ⇒ Object
Description Load hash object into the global configuration space.
Instance Method Summary collapse
-
#add(section_name, directive_name, value) ⇒ Object
Description Add a configuration item to configuration object.
-
#initialize ⇒ Config
constructor
:nodoc:.
Constructor Details
#initialize ⇒ Config
:nodoc:
107 108 109 |
# File 'lib/jewelbox/config.rb', line 107 def initialize # :nodoc: @section = {} end |
Class Method Details
.load(yml_path) ⇒ Object
Description
Read the given YAML file, and add its content to Jewelbox.config. For example, if you had the following in the YAML file:
section1:
directive1: true
directive2: false
section2:
directive1: "hello"
directive2: "good-bye"
The following config items are available:
Jewelbox.config.section1.directive1 # => true Jewelbox.config.section1.directive2 # => true Jewelbox.config.section1.directive1 # => “hello” Jewelbox.config.section2.directive2 # => “there”
Parameters
- yml_path
- String
-
absolute or relative path to the YAML file
71 72 73 74 75 |
# File 'lib/jewelbox/config.rb', line 71 def self.load(yml_path) abs_path = File.(yml_path) h = YAML::load(File.read(yml_path)) load_hash(h) end |
.load_hash(h) ⇒ Object
Description
Load hash object into the global configuration space. See Config.load API, which has example hash object.
Parameters
- h
- Hash
-
Hash object that contains 2-level configuration organization (section/directive) to load into Jewelbox.config space.
85 86 87 88 89 90 91 |
# File 'lib/jewelbox/config.rb', line 85 def self.load_hash(h) h.each do |section, inner| inner.each do |directive, v| Jewelbox.config.add(section, directive, v) end end end |
Instance Method Details
#add(section_name, directive_name, value) ⇒ Object
Description
Add a configuration item to configuration object
Parameters
- section_name
- String
-
section name
- directive_name
- String
-
directive name
- value
- Object
-
configuration value
Returns
Config object self
122 123 124 125 126 127 128 129 130 |
# File 'lib/jewelbox/config.rb', line 122 def add(section_name, directive_name, value) section = @section[section_name] if section.nil? section = (@section[section_name] = Section.new(section_name)) eigenclass.send(:define_method, section_name) { section } end section.add(directive_name, value) self end |