summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitry Kim <dmitry point kim at gmail point com>2007-10-31 19:42:24 +0000
committerDmitry Kim <dmitry point kim at gmail point com>2007-10-31 19:42:24 +0000
commit1ad496b1f55715c33593b545299f5c5e877e25f0 (patch)
tree1017ab08d9d188f1381a0e457d316c5e9b4d9cbe /lib
parent4a064fbe3e95e4e6bac6e7a3c2e38d4bbb9f5f87 (diff)
* (rbot/config) slightly less ugly ArrayValue validation interface
Diffstat (limited to 'lib')
-rw-r--r--lib/rbot/config.rb41
1 files changed, 30 insertions, 11 deletions
diff --git a/lib/rbot/config.rb b/lib/rbot/config.rb
index 7ed019fe..360cba01 100644
--- a/lib/rbot/config.rb
+++ b/lib/rbot/config.rb
@@ -96,16 +96,18 @@ module Config
get.to_s
end
- private
- def validate(value)
- return true unless @validate
- if @validate.instance_of?(Proc)
- return @validate.call(value)
- elsif @validate.instance_of?(Regexp)
- raise ArgumentError, "validation via Regexp only supported for strings!" unless value.instance_of? String
- return @validate.match(value)
+ protected
+ def validate(val, validator = @validate)
+ case validator
+ when false, nil
+ return true
+ when Proc
+ return validator.call(val)
+ when Regexp
+ raise ArgumentError, "validation via Regexp only supported for strings!" unless String === val
+ return validator.match(val)
else
- raise ArgumentError, "validation type #{@validate.class} not supported"
+ raise ArgumentError, "validation type #{validator.class} not supported"
end
end
end
@@ -157,6 +159,18 @@ module Config
end
class ArrayValue < Value
+ def initialize(key, params)
+ super
+ @validate_item = params[:validate_item]
+ @validate ||= Proc.new do |v|
+ !v.find { |i| !validate_item(i) }
+ end
+ end
+
+ def validate_item(item)
+ validate(item, @validate_item)
+ end
+
def parse(string)
string.split(/,\s+/)
end
@@ -164,8 +178,13 @@ module Config
get.join(", ")
end
def add(val)
- curval = self.get
- set(curval + [val]) unless curval.include?(val)
+ newval = self.get.dup
+ unless newval.include? val
+ newval << val
+ validate_item(val) or raise ArgumentError, "invalid item: #{val}"
+ validate(newval) or raise ArgumentError, "invalid value: #{newval.to_s}"
+ set(newval)
+ end
end
def rm(val)
curval = self.get