Class: Aequitas::Rule::Length
- Inherits:
-
Aequitas::Rule
- Object
- Aequitas::Rule
- Aequitas::Rule::Length
- Defined in:
- lib/aequitas/rule/length.rb,
lib/aequitas/rule/length/equal.rb,
lib/aequitas/rule/length/range.rb,
lib/aequitas/rule/length/maximum.rb,
lib/aequitas/rule/length/minimum.rb
Defined Under Namespace
Classes: Dummy, Equal, Maximum, Minimum, Range
Instance Attribute Summary
Attributes inherited from Aequitas::Rule
#attribute_name, #custom_message, #guard, #skip_condition
Attributes included from ValueObject
Class Method Summary (collapse)
-
+ (Object) rules_for(attribute_name, options)
TODO: move options normalization into the validator macros.
Instance Method Summary (collapse)
- - (Boolean) expected_length?(length)
- - (Boolean) valid_value?(value)
-
- (Object) value_length(value)
calculate length of multi-byte-encoded strings.
Methods inherited from Aequitas::Rule
#attribute_value, #execute?, #initialize, #skip?, #validate, #violation_data, #violation_info, #violation_values
Methods included from ValueObject
Constructor Details
This class inherits a constructor from Aequitas::Rule
Class Method Details
+ (Object) rules_for(attribute_name, options)
TODO: move options normalization into the validator macros
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/aequitas/rule/length.rb', line 10 def self.rules_for(attribute_name, ) = .dup equal = .values_at(:is, :equals).compact.first range = .values_at(:in, :within).compact.first minimum = .values_at(:min, :minimum).compact.first maximum = .values_at(:max, :maximum).compact.first if minimum && maximum range ||= minimum..maximum end rule = if equal Length::Equal.new(attribute_name, .merge(:expected => equal)) elsif range Length::Range.new(attribute_name, .merge(:range => range)) elsif minimum Length::Minimum.new(attribute_name, .merge(:bound => minimum)) elsif maximum Length::Maximum.new(attribute_name, .merge(:bound => maximum)) else # raise ArgumentError, "expected one of :is, :equals, :within, :in, :minimum, :min, :maximum, or :max; got #{options.keys.inspect}" warn "expected length specification: one of :is, :equals, :in, :within, :min, :minimum, :max, or :maximum; got #{.keys.inspect}" Length::Dummy.new(attribute_name, ) end [rule] end |
Instance Method Details
- (Boolean) expected_length?(length)
47 48 49 |
# File 'lib/aequitas/rule/length.rb', line 47 def expected_length?(length) raise NotImplementedError, "#{self.class}#valid_length? must be implemented" end |
- (Boolean) valid_value?(value)
43 44 45 |
# File 'lib/aequitas/rule/length.rb', line 43 def valid_value?(value) skip?(value) || expected_length?(value_length(value)) end |
- (Object) value_length(value)
calculate length of multi-byte-encoded strings
as characters rather than bytes
62 63 64 |
# File 'lib/aequitas/rule/length.rb', line 62 def value_length(value) value.to_s.scan(/./u).size end |