WithValidations
Easy validation of one or more option keys and values using a single method.
Features
- Validate If all option values are valid.
- Automatically assign default values to missing but required keys.
- (Optionally) check for unsupported keys in the options hash.
- Assign the validated values to one or more variables.
Installation
$ gem install with_validations
Usage
Note: A class that includes this module is required to:
- provide a constant named
OPTIONSwith a hash of the following type:{ option_key => [default_value, validation_proc], ...}. - name the optional option hash of a method
options.
class TestClass
include 'Options'
# option_key => [default_value, validation_proc]
OPTIONS = {:compact => [false, lambda {|value| is_boolean?(value) }],
:with_pinyin => [true, lambda {|value| is_boolean?(value) }],
:thread_count => [8, lambda {|value| value.kind_of?(Integer) }]}
def test_method_1(={})
@thread_count = validate { :thread_count }
# ...
end
def self.test_method_2(={})
# Optional argument set to true: Throw exception if the options hash contains any
# unsupported keys.
@compact, @with_pinyin = validate(true) { [:compact, :with_pinyin] }
# ...
end
#...
end
my_class = TestClass.new
my_class.test_method_1(:thread_count => 10)
# @thread_count is set to 10
my_class.test_method_1(:thread_count => 10)
# @thread_count is set to 10
my_class.test_method_1(:thread_count => 10)
# @thread_count is set to 10
TestClass.test_method2(:compact => true, :with_pinyin => false)
# @compact is set to true, @with_pinyin is set to false
TestClass.test_method2
# No options provided: The default values from OPTIONS will be used
# @compact is set to false, @with_pinyin is set to true
TestClass.test_method2(:compact => true, :with_pinyin => false)
# @compact is set to true, @with_pinyin is set to false
Documentation
Main method
-
Optional methods
Note: All methods are available as both instance and singleton methods.