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

.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, 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

#valid?(resource) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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