| 1 |
# config_manager.rb: the class definition of Edmaru::ConfigManager |
| 2 |
# |
| 3 |
# Copyright (C) 2007 Takashi Nakamoto |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, but |
| 10 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
# 02110-1301 USA. |
| 18 |
# |
| 19 |
|
| 20 |
require "config/config_element.rb" |
| 21 |
require "config/standard_validators.rb" |
| 22 |
|
| 23 |
module Edmaru::Config |
| 24 |
|
| 25 |
#The instance of this class manages the whole configurations of Edmaru. |
| 26 |
class ConfigManager |
| 27 |
DEFAULT_CONFIG_FILES = |
| 28 |
["gtk/gtk_config.rb", |
| 29 |
"ncurses/ncurses_config.rb"] |
| 30 |
@@default_elements = Array.new |
| 31 |
|
| 32 |
#Initilizer. |
| 33 |
# |
| 34 |
#=== Return |
| 35 |
#An initilized instance of Edmaru::ConfigManager. |
| 36 |
def initialize |
| 37 |
@hash = Hash.new |
| 38 |
|
| 39 |
DEFAULT_CONFIG_FILES.each{ |file| |
| 40 |
require file |
| 41 |
} |
| 42 |
@@default_elements.each{ |element| |
| 43 |
add_element(element) |
| 44 |
} |
| 45 |
end |
| 46 |
|
| 47 |
#Add a new element. |
| 48 |
def add_element(element) |
| 49 |
@hash[element.key] = element |
| 50 |
end |
| 51 |
|
| 52 |
#Changed the specified configuration value. |
| 53 |
def []=(key, val) |
| 54 |
if @hash.include?(key) |
| 55 |
@hash[key].value = val |
| 56 |
else |
| 57 |
#ToDo: How should we deal with unrecognized configuration keys? |
| 58 |
raise "Unknown key: #{key}" |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
#Get the specified configuration value. |
| 63 |
def [](key) |
| 64 |
if @hash.include?(key) |
| 65 |
return @hash[key].value |
| 66 |
else |
| 67 |
return nil |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
#Set multiple configuration values. |
| 72 |
# |
| 73 |
#e.g. |
| 74 |
# config << { |
| 75 |
# "/UI/Gtk/Font" => "Monospace 10", |
| 76 |
# "/UI/Gtk/Color/Background" => "PapayaWhip" |
| 77 |
# } |
| 78 |
# |
| 79 |
#=== Arguments |
| 80 |
#_hash_ :: An instance of Hash that contains multiple pairs of |
| 81 |
#configuration key and value. |
| 82 |
def <<(hash) |
| 83 |
hash.each{ |key, val| |
| 84 |
self[key] = val |
| 85 |
} |
| 86 |
end |
| 87 |
end |
| 88 |
end |