Class: Glyph::Config
Overview
The Glyph::Config class is used (you don’t say!) to store configuration data. Essentially it wraps a Hash of Hashes and provides some useful methods to access keys and subkeys.
Instance Method Summary collapse
-
#get(setting) ⇒ Object
Returns a configuration setting.
-
#initialize(options = {}) ⇒ Config
constructor
Initializes the configuration with a hash of options: * :file (default: nil) - A YAML file to read data from * :data (default: {})- The initial contents * :resettable (default: false) - Whether the configuration can be reset (cleared) or not * :mutable (default: true) - Whether the configuration can be changed or not.
-
#merge(cfg) ⇒ Glyph::Config
Merges configuration data by applying Hash#merge to each sub-hash of data, recursively.
-
#read ⇒ Hash
Reads the contents of a file and stores them as configuration data.
-
#reset(hash = {}) ⇒ Hash
Resets all configuration data.
-
#set(setting, value) ⇒ Object
Updates a configuration setting.
-
#to_hash ⇒ Hash
Returns the underlying data hash.
-
#update(cfg) ⇒ Object
(also: #merge!)
Updates configuration data by applying Hash#update to each sub-hash of data, recursively.
-
#write ⇒ Object
Serialize configuration data and writes it to a file.
Methods included from Utils
#clean_xml_document, #complex_output?, #current_output_setting, #debug, #error, #file_copy, #file_load, #file_write, #info, #load_files_from_dir, #macro_alias?, #macro_aliases_for, #macro_definition_for, #macro_eq?, #msg, #multiple_output_files?, #project?, #run_external_command, #titled_sections, #warning, #with_files_from, #yaml_dump, #yaml_load
Constructor Details
#initialize(options = {}) ⇒ Config
Initializes the configuration with a hash of options:
-
:file (default: nil) - A YAML file to read data from
-
:data (default: {})- The initial contents
-
:resettable (default: false) - Whether the configuration can be reset (cleared) or not
-
:mutable (default: true) - Whether the configuration can be changed or not
18 19 20 21 22 23 24 |
# File 'lib/glyph/config.rb', line 18 def initialize(={}) = {:file => nil, :data => {}, :resettable => false, :mutable => true} @options = .merge @file = @options[:file] @data = @options[:data] read if @file end |
Instance Method Details
#get(setting) ⇒ Object
Returns a configuration setting
131 132 133 |
# File 'lib/glyph/config.rb', line 131 def get(setting) @data.instance_eval "self#{setting.to_s.split(".").map{|key| "[:#{key}]" }.join}" rescue nil end |
#merge(cfg) ⇒ Glyph::Config
Merges configuration data by applying Hash#merge to each sub-hash of data, recursively.
54 55 56 |
# File 'lib/glyph/config.rb', line 54 def merge(cfg) merge_or_update cfg, :merge end |
#read ⇒ Hash
Reads the contents of a file and stores them as configuration data
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/glyph/config.rb', line 63 def read raise RuntimeError, "Configuration is not stored in a file." if @file.blank? if @file.exist? then contents = yaml_load @file raise RuntimeError, "Invalid configuration file '#{@file}'" unless contents.is_a? Hash @data = contents else @data = {} end @data end |
#reset(hash = {}) ⇒ Hash
Resets all configuration data
36 37 38 39 40 |
# File 'lib/glyph/config.rb', line 36 def reset(hash={}) raise RuntimeError, "Configuration cannot be reset" unless @options[:resettable] raise RuntimeError, "Configuration data is not stored in a Hash" unless hash.is_a? Hash @data = hash end |
#set(setting, value) ⇒ Object
Updates a configuration setting
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/glyph/config.rb', line 87 def set(setting, value) raise RuntimeError, "Configuration cannot be changed" unless @options[:mutable] if value.is_a?(String) && value.match(/^(["'].*["']|:.+|\[.*\]|\{.*\}|true|false|nil)$/) then value = Kernel.instance_eval value end hash = @data path = setting.to_s.split(".").map{|s| s.intern} count = 1 path.each do |s| if hash.has_key? s then if count == path.length then # destination hash[s] = value else if hash[s].is_a?(Hash) then hash = hash[s] count +=1 else raise ArgumentError, "Invalid namespace #{s}" end end else # key not found if count == path.length then # destination hash[s] = value else # create a new namespace hash[s] = {} hash = hash[s] count +=1 end end end value end |
#to_hash ⇒ Hash
Returns the underlying data hash
28 29 30 |
# File 'lib/glyph/config.rb', line 28 def to_hash @data end |
#update(cfg) ⇒ Object Also known as: merge!
Updates configuration data by applying Hash#update to each sub-hash of data, recursively.
46 47 48 |
# File 'lib/glyph/config.rb', line 46 def update(cfg) merge_or_update cfg, :update end |
#write ⇒ Object
Serialize configuration data and writes it to a file
137 138 139 140 |
# File 'lib/glyph/config.rb', line 137 def write raise RuntimeError, "Configuration is not stored in a file." if @file.blank? yaml_dump @file, @data end |