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
-
.rules_for(attribute_name, options) ⇒ Object
TODO: move options normalization into the validator macros.
Instance Method Summary collapse
- #valid?(resource) ⇒ Boolean
- #valid_length?(length) ⇒ Boolean
-
#value_length(value) ⇒ Object
calculate length of multi-byte-encoded strings as characters rather than bytes.
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
.rules_for(attribute_name, options) ⇒ Object
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
#valid?(resource) ⇒ Boolean
43 44 45 46 47 |
# File 'lib/aequitas/rule/length.rb', line 43 def valid?(resource) value = attribute_value(resource) skip?(value) || valid_length?(value_length(value)) end |
#valid_length?(length) ⇒ Boolean
49 50 51 |
# File 'lib/aequitas/rule/length.rb', line 49 def valid_length?(length) raise NotImplementedError, "#{self.class}#valid_length? must be implemented" end |
#value_length(value) ⇒ Object
calculate length of multi-byte-encoded strings
as characters rather than bytes
64 65 66 67 68 |
# File 'lib/aequitas/rule/length.rb', line 64 def value_length(value) value.length rescue NoMethodError value.to_s.length end |