| 1 |
# standard_validators.rb: an assembly of pre-defined validators for |
| 2 |
# Edmaru::Config::ConfigElement. |
| 3 |
# |
| 4 |
# Copyright (C) 2007 Takashi Nakamoto |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License version 2 as |
| 8 |
# published by the Free Software Foundation. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
# 02110-1301 USA. |
| 19 |
# |
| 20 |
|
| 21 |
module Edmaru::Config |
| 22 |
module Validators |
| 23 |
|
| 24 |
#This validator accept an arbitrary string. |
| 25 |
STRING_VALIDATOR = Proc.new{ |value| |
| 26 |
raise "Not an instance of String was passed." if value.class != String |
| 27 |
} |
| 28 |
|
| 29 |
#This validator accept objects included in the specified array. |
| 30 |
#To get a validator that accepts any one of "apple", "banana" or "orange", |
| 31 |
#call this method like the following: |
| 32 |
# |
| 33 |
# LIST_VALIDATOR(["apple", "banana", "orange"]) |
| 34 |
def Validators.LIST_VALIDATOR(array) |
| 35 |
Proc.new{ |value| |
| 36 |
if !array.include?(value) |
| 37 |
raise "#{value} is invalid." |
| 38 |
end |
| 39 |
} |
| 40 |
end |
| 41 |
end |
| 42 |
end |