Class: Strelka::ParamValidator::Constraint
- Inherits:
-
Object
- Object
- Strelka::ParamValidator::Constraint
- Extended by:
- Loggability, MethodUtilities
- Defined in:
- lib/strelka/paramvalidator.rb
Overview
The base constraint type.
Direct Known Subclasses
Constant Summary collapse
- FLAGS =
Flags that are passed as Symbols when declaring a parameter
[ :required, :multiple ]
- TYPES =
Map of constraint specification types to their equivalent Constraint class.
{ Proc => self }
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
The constraint's check block.
-
#description ⇒ Object
Get the description of the field.
-
#name ⇒ Object
readonly
The name of the field the constraint governs.
Class Method Summary collapse
-
.for(field, spec = nil, *options, &block) ⇒ Object
Return a Constraint object appropriate for the given
fieldandspec. -
.register_type(syntax_class) ⇒ Object
Register the given
subclassas the Constraint class to be used when the specifiedsyntax_classis given as the constraint in a parameter declaration.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Comparison operator – Constraints are equal if they’re for the same field, they’re of the same type, and their blocks are the same.
-
#apply(value) ⇒ Object
Check the given value against the constraint and return the result if it passes.
-
#initialize(name, *args, &block) ⇒ Constraint
constructor
Create a new Constraint for the field with the given
name, configuring it with the specifiedargs. -
#multiple? ⇒ Object
Returns true if the field can have multiple values.
-
#required? ⇒ Object
Returns true if the field associated with the constraint is required in order for the parameters to be valid.
-
#to_s ⇒ Object
Return the constraint expressed as a String.
Methods included from MethodUtilities
attr_predicate, attr_predicate_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader
Constructor Details
#initialize(name, *args, &block) ⇒ Constraint
Create a new Constraint for the field with the given name, configuring it with the
specified args. The block is what does the actual validation, at least in the
base class.
117 118 119 120 121 122 123 124 125 |
# File 'lib/strelka/paramvalidator.rb', line 117 def initialize( name, *args, &block ) @name = name @block = block @description = args.shift if args.first.is_a?( String ) @required = args.include?( :required ) @multiple = args.include?( :multiple ) end |
Instance Attribute Details
#block ⇒ Object (readonly)
The constraint's check block
136 137 138 |
# File 'lib/strelka/paramvalidator.rb', line 136 def block @block end |
#description ⇒ Object
Get the description of the field.
171 172 173 |
# File 'lib/strelka/paramvalidator.rb', line 171 def description return @description || self.generate_description end |
#name ⇒ Object (readonly)
The name of the field the constraint governs
133 134 135 |
# File 'lib/strelka/paramvalidator.rb', line 133 def name @name end |
Class Method Details
.for(field, spec = nil, *options, &block) ⇒ Object
Return a Constraint object appropriate for the given field and spec.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/strelka/paramvalidator.rb', line 96 def self::for( field, spec=nil, *, &block ) self.log.debug "Building Constraint for %p (%p)" % [ field, spec ] # Handle omitted constraint if spec.is_a?( String ) || FLAGS.include?( spec ) .unshift( spec ) spec = nil end spec ||= block subtype = TYPES[ spec.class ] or raise "No constraint type for a %p validation spec" % [ spec.class ] return subtype.new( field, spec, *, &block ) end |
.register_type(syntax_class) ⇒ Object
Register the given subclass as the Constraint class to be used when
the specified syntax_class is given as the constraint in a parameter
declaration.
88 89 90 91 92 |
# File 'lib/strelka/paramvalidator.rb', line 88 def self::register_type( syntax_class ) self.log.debug "Registering %p as the constraint class for %p objects" % [ self, syntax_class ] TYPES[ syntax_class ] = self end |
Instance Method Details
#==(other) ⇒ Object
Comparison operator – Constraints are equal if they’re for the same field, they’re of the same type, and their blocks are the same.
163 164 165 166 167 |
# File 'lib/strelka/paramvalidator.rb', line 163 def ==( other ) return self.name == other.name && other.instance_of?( self.class ) && self.block == other.block end |
#apply(value) ⇒ Object
Check the given value against the constraint and return the result if it passes.
152 153 154 155 156 157 158 |
# File 'lib/strelka/paramvalidator.rb', line 152 def apply( value ) if self.multiple? return self.check_multiple( value ) else return self.check( value ) end end |
#multiple? ⇒ Object
Returns true if the field can have multiple values.
143 |
# File 'lib/strelka/paramvalidator.rb', line 143 attr_predicate :multiple? |
#required? ⇒ Object
Returns true if the field associated with the constraint is required in order for the parameters to be valid.
148 |
# File 'lib/strelka/paramvalidator.rb', line 148 attr_predicate :required? |
#to_s ⇒ Object
Return the constraint expressed as a String.
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/strelka/paramvalidator.rb', line 177 def to_s desc = self.validator_description flags = [] flags << 'required' if self.required? flags << 'multiple' if self.multiple? desc << " (%s)" % [ flags.join(',') ] unless flags.empty? return desc end |