| 1 |
# config_element.rb: the class definition of Edmaru::ConfigElement |
| 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 |
module Edmaru::Config |
| 21 |
|
| 22 |
#Instances of this class represents one configuration. |
| 23 |
class ConfigElement |
| 24 |
|
| 25 |
#Initializer. |
| 26 |
# |
| 27 |
#=== Arguments |
| 28 |
#_key_ :: The unique key of this element. |
| 29 |
#_default_ :: Default value. |
| 30 |
#_desc_ :: Description. |
| 31 |
#_validator_ :: An instance of Proc. |
| 32 |
# |
| 33 |
#=== About _key_ |
| 34 |
#The _key_ should have hierarchical structure. So all hierarycies |
| 35 |
#should be separated by slash characters("/") and it should start |
| 36 |
#with a slash character("/") like an absolute path of Unix. |
| 37 |
# |
| 38 |
#For example: "/UI/Gtk/Font" |
| 39 |
# |
| 40 |
#=== About _validator_ |
| 41 |
#ToDo: write about _validator_ |
| 42 |
# |
| 43 |
#=== Return |
| 44 |
#An initialized instance of Edmaru::ConfigElement. |
| 45 |
def initialize(key, default, desc, validator) |
| 46 |
@key = key |
| 47 |
@default_value = default |
| 48 |
@use_default = true |
| 49 |
@description = desc |
| 50 |
@validator = validator |
| 51 |
end |
| 52 |
|
| 53 |
def key |
| 54 |
@key |
| 55 |
end |
| 56 |
|
| 57 |
def value |
| 58 |
if @use_default |
| 59 |
return @default_value |
| 60 |
else |
| 61 |
return @value |
| 62 |
end |
| 63 |
end |
| 64 |
|
| 65 |
def value=(new_value) |
| 66 |
#Check if the passed value is valid or not. |
| 67 |
begin |
| 68 |
@validator.call(new_value) |
| 69 |
rescue => e |
| 70 |
raise e |
| 71 |
end |
| 72 |
|
| 73 |
@use_default = false |
| 74 |
@value = new_value |
| 75 |
end |
| 76 |
|
| 77 |
def description |
| 78 |
@description |
| 79 |
end |
| 80 |
end |
| 81 |
end |