Class: Aequitas::Rule::Length

Inherits:
Aequitas::Rule show all
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

Direct Known Subclasses

Dummy, Equal, Maximum, Minimum, Range

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

#equalizer

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Aequitas::Rule

#attribute_value, #execute?, #initialize, #skip?, #validate, #violation_data, #violation_info, #violation_values

Methods included from ValueObject

#equalize_on

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, options)
  options = options.dup

  equal   = options.values_at(:is,  :equals).compact.first
  range   = options.values_at(:in,  :within).compact.first
  minimum = options.values_at(:min, :minimum).compact.first
  maximum = options.values_at(:max, :maximum).compact.first

  if minimum && maximum
    range ||= minimum..maximum
  end

  rule =
    if equal
      Length::Equal.new(attribute_name,   options.merge(:expected => equal))
    elsif range
      Length::Range.new(attribute_name,   options.merge(:range => range))
    elsif minimum
      Length::Minimum.new(attribute_name, options.merge(:bound => minimum))
    elsif maximum
      Length::Maximum.new(attribute_name, options.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 #{options.keys.inspect}"
      Length::Dummy.new(attribute_name, options)
    end

  [rule]
end

Instance Method Details

- (Boolean) expected_length?(length)

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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)

Returns:

  • (Boolean)


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