lib/tap/support/validation.rb in bahuvrihi-tap-0.10.7 vs lib/tap/support/validation.rb in bahuvrihi-tap-0.10.8
- old
+ new
@@ -2,13 +2,13 @@
module Tap
module Support
# Validation generates blocks for common validations and transformations of
- # configurations set through Configurable. In general these blocks allow
- # configurations to be set to objects of a particular class, or to a string
- # that can be loaded as YAML into such an object.
+ # configurations set through Configurable. In general these blocks load
+ # string inputs as YAML and valdiate the results; non-string inputs are
+ # simply validated.
#
# integer = Validation.integer
# integer.class # => Proc
# integer.call(1) # => 1
# integer.call('1') # => 1
@@ -21,11 +21,10 @@
# block = lambda {}
# CONST = block
#
# This syntax plays well with RDoc, which otherwise gets jacked
# when you do it all in one step.
- #++
module Validation
# Raised when Validation blocks fail.
class ValidationError < ArgumentError
def initialize(input, validations)
@@ -202,13 +201,15 @@
# boolean.call("str") # => ValidationError
#
def boolean(); BOOLEAN; end
BOOLEAN = yamlize_and_check(true, false, nil)
+ # Same as boolean.
def switch(); SWITCH; end
SWITCH = yamlize_and_check(true, false, nil)
+ # Same as boolean.
def flag(); FLAG; end
FLAG = yamlize_and_check(true, false, nil)
# Returns a block that checks the input is an array.
# String inputs are loaded as yaml first.
@@ -227,17 +228,24 @@
# array_or_nil.call('~') # => nil
# array_or_nil.call(nil) # => nil
def array_or_nil(); ARRAY_OR_NIL; end
ARRAY_OR_NIL = yamlize_and_check(Array, nil)
+ # Returns a block that checks the input is an array.
+ # If the input is a string the string is split along
+ # commas and each value yamlized into an array.
+ #
+ # list.class # => Proc
+ # list.call([1,2,3]) # => [1,2,3]
+ # list.call('1,2,3') # => [1,2,3]
+ # list.call('str') # => ['str']
+ # list.call(nil) # => ValidationError
+ #
def list(); LIST; end
list_block = lambda do |input|
if input.kind_of?(String)
- input = case processed_input = yamlize(input)
- when Array then processed_input
- else input.split(/,/).collect {|arg| yamlize(arg) }
- end
+ input = input.split(/,/).collect {|arg| yamlize(arg) }
end
validate(input, [Array])
end
LIST = list_block
@@ -436,14 +444,15 @@
# method (autoload doesn't work since Time already exists)
require 'time' unless Time.respond_to?(:parse)
TIME
end
- TIME = lambda do |input|
+ time_block = lambda do |input|
input = Time.parse(input) if input.kind_of?(String)
validate(input, [Time])
end
+ TIME = time_block
# Same as time but allows nil:
#
# time_or_nil.call('~') # => nil
# time_or_nil.call(nil) # => nil
@@ -452,17 +461,19 @@
# method (autoload doesn't work since Time already exists)
require 'time' unless Time.respond_to?(:parse)
TIME_OR_NIL
end
- TIME_OR_NIL = lambda do |input|
+ time_or_nil_block = lambda do |input|
input = case input
when nil, '~' then nil
when String then Time.parse(input)
else input
end
validate(input, [Time, nil])
- end
+ end
+ TIME_OR_NIL = time_or_nil_block
+
end
end
end
\ No newline at end of file