Module: Waw::Validation
- Extended by:
- Helpers
- Included in:
- StaticController::WawAccess::DSL
- Defined in:
- lib/waw/validation.rb,
lib/waw/validation/errors.rb,
lib/waw/validation/helpers.rb,
lib/waw/validation/signature.rb,
lib/waw/validation/validator.rb,
lib/waw/validation/or_validator.rb,
lib/waw/validation/and_validator.rb,
lib/waw/validation/not_validator.rb,
lib/waw/validation/date_validator.rb,
lib/waw/validation/file_validator.rb,
lib/waw/validation/isin_validator.rb,
lib/waw/validation/same_validator.rb,
lib/waw/validation/float_validator.rb,
lib/waw/validation/regexp_validator.rb,
lib/waw/validation/size_validations.rb,
lib/waw/validation/string_validator.rb,
lib/waw/validation/array_validations.rb,
lib/waw/validation/boolean_validator.rb,
lib/waw/validation/default_validator.rb,
lib/waw/validation/integer_validator.rb,
lib/waw/validation/missing_validator.rb,
lib/waw/validation/datetime_validator.rb,
lib/waw/validation/mandatory_validator.rb,
lib/waw/validation/comparison_validations.rb
Overview
Provides a reusable architecture for parameter validation
Defined Under Namespace
Modules: ArrayValidations, ComparisonValidations, Helpers, SizeValidations Classes: AndValidator, BooleanValidator, DateTimeValidator, DateValidator, DefaultValidator, Error, FileValidator, FloatValidator, IntegerValidator, IsInValidator, KO, MandatoryValidator, MissingValidator, NotValidator, OrValidator, RegexpValidator, SameValidator, Signature, StringValidator, Validator
Constant Summary collapse
- Array =
Validators from other modules
::Waw::Validation::ArrayValidations
- @@validators =
Validators, by name
{}
- @@ruby_classes_to_validators =
Ruby classes => validators
{}
- @@who_includes_me =
Other modules that include me
[]
Class Method Summary collapse
-
.included(who) ⇒ Object
When this module is included in who we install all validation methods dynamically.
-
.method_missing(name, *args, &block) ⇒ Object
User-friendly message for missing validators.
-
.ruby_class_to_validator(ruby_class, validator) ⇒ Object
Adds a ruby class => validator mapping.
-
.signature(&block) ⇒ Object
Builds a signature with a given block as definition.
-
.validator(name = nil, *args, &block) ⇒ Object
Automatically builds the validator and installs a method returning it.
-
.validator_for_ruby_class(ruby_class, raise_if_not_found = true) ⇒ Object
Returns a validator to use for a given ruby class.
Methods included from Helpers
all_missing?, any_missing?, argument_safe, error, is_missing?, missings_to_nil, no_missing?, to_validator
Class Method Details
.included(who) ⇒ Object
When this module is included in who we install all validation methods dynamically
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/waw/validation.rb', line 51 def included(who) @@who_includes_me << who unless @@who_includes_me.include?(who) @@validators.each_pair do |name, validator| who.module_eval <<-EOF def #{name}(*args, &block) ::Waw::Validation.send(:#{name}, *args, &block) end EOF end end |
.method_missing(name, *args, &block) ⇒ Object
User-friendly message for missing validators
134 135 136 |
# File 'lib/waw/validation.rb', line 134 def method_missing(name, *args, &block) raise WawError, "Unknown validator #{name}", caller end |
.ruby_class_to_validator(ruby_class, validator) ⇒ Object
Adds a ruby class => validator mapping
74 75 76 |
# File 'lib/waw/validation.rb', line 74 def ruby_class_to_validator(ruby_class, validator) @@ruby_classes_to_validators[ruby_class] = validator end |
.signature(&block) ⇒ Object
Builds a signature with a given block as definition
69 70 71 |
# File 'lib/waw/validation.rb', line 69 def signature(&block) Signature.new(&block) end |
.validator(name = nil, *args, &block) ⇒ Object
Automatically builds the validator and installs a method returning it.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/waw/validation.rb', line 87 def validator(name = nil, *args, &block) # Build the validator validator = args[0] if validator.nil? and block # first case, block creation validator = Validator.new(&block) elsif ::Waw::Validation::Validator===validator and block.nil? # first case: explicit validator, nothing to do elsif validator.is_a?(::Class) and block.nil? # second case: class, for defered creation validator = to_validator(validator) unless validator.ancestors.include?(::Waw::Validation::Validator) elsif ::Module===validator and block.nil? # third case: module of validation rules, nothing to do elsif block.nil? # fifth case: auto conversion validator = to_validator(validator) else raise WawError, "Invalid Waw::Validation.validator call with #{name}, #{args.inspect}, #{block.inspect}" end # Install it if a name is provided return validator if name.nil? @@validators[name] = validator # Installs the class method that returns the validator instance_eval <<-EOF class << self def #{name}(*args, &block) val = @@validators[:#{name}] val.is_a?(::Class) ? val.new(*args, &block) : val end end EOF # Install the class methods on all modules that include me @@who_includes_me.each do |mod| mod.module_eval <<-EOF def #{name}(*args, &block) ::Waw::Validation.send(:#{name}, *args, &block) end EOF end validator end |
.validator_for_ruby_class(ruby_class, raise_if_not_found = true) ⇒ Object
Returns a validator to use for a given ruby class
79 80 81 82 83 |
# File 'lib/waw/validation.rb', line 79 def validator_for_ruby_class(ruby_class, raise_if_not_found=true) val = @@ruby_classes_to_validators[ruby_class] return val unless val.nil? raise "Unable to find a validator for #{ruby_class}" if raise_if_not_found end |