Class: Glyph::Config

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/glyph/config.rb

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

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

Parameters:

  • options (Hash) (defaults to: {})

    the configuration options (merged with the the defaults)


18
19
20
21
22
23
24
# File 'lib/glyph/config.rb', line 18

def initialize(options={})
	default_options = {:file => nil, :data => {}, :resettable => false, :mutable => true}
	@options = default_options.merge options
	@file = @options[:file]
	@data = @options[:data]
	read if @file
end

Instance Method Details

#get(setting) ⇒ Object

Returns a configuration setting

Examples:

cfg = Glyph::Config.new
cfg.get "system.quiet"							# true
cfg.get "test.test_value"	# [1,2,3]

Parameters:

Returns:

  • (Object)

    the new value

See Also:


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.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    unless cfg is a Glyph::Config


54
55
56
# File 'lib/glyph/config.rb', line 54

def merge(cfg)
	merge_or_update cfg, :merge
end

#readHash

Reads the contents of a file and stores them as configuration data

Returns:

  • (Hash)

    Configuration data

Raises:

  • (RuntimeError)

    if self is not linked to a file or if the file does not contain a serialized Hash


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

Parameters:

  • hash (Hash) (defaults to: {})

    the new configuration data to store

Returns:

  • (Hash)

    Configuration data

Raises:

  • (RuntimeError)

    unless the configuration is resettable or if no hash is passed


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

Examples:

cfg = Glyph::Config.new
cfg.set "system.quiet", true 								# Sets "system.quiet" => true
cfg.set "test.test_value", "[1,2,3]" # Sets :test => {:test_value => [1,2,3]}
cfg.set "system.quiet", "false" 							# Sets "system.quiet" => false

Parameters:

  • setting (String, Symbol)

    the setting to update

  • value (Object)

    the value to store. Where applicable (Array, Hash, Boolean, Nil), attempts to evaluate string values

Returns:

  • (Object)

    the new value

Raises:

  • (RuntimeError)

    unless the configuration is mutable

  • (ArgumentError)

    if the setting refers to an invalid namespace


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_hashHash

Returns the underlying data hash

Returns:

  • (Hash)

    Configuration data


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.

Parameters:

Returns:

  • self

Raises:

  • (ArgumentError)

    unless cfg is a Glyph::Config


46
47
48
# File 'lib/glyph/config.rb', line 46

def update(cfg)
	merge_or_update cfg, :update
end

#writeObject

Serialize configuration data and writes it to a file

Raises:

  • (RuntimeError)

    if the configuration is not linked 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