Class: AWS::Record::InclusionValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/aws/record/validators/inclusion.rb

Direct Known Subclasses

ExclusionValidator

Constant Summary collapse

ACCEPTED_OPTIONS =
[:in, :message, :allow_nil, :on, :if, :unless]

Instance Attribute Summary

Attributes inherited from Validator

#attribute_names, #options

Instance Method Summary collapse

Methods inherited from Validator

#initialize, #validate

Constructor Details

This class inherits a constructor from AWS::Record::Validator

Instance Method Details

#messageObject


49
50
51
# File 'lib/aws/record/validators/inclusion.rb', line 49

def message
  options[:message] || 'is not included in the list'
end

#setup(record_class) ⇒ Object


24
25
26
27
# File 'lib/aws/record/validators/inclusion.rb', line 24

def setup record_class
  ensure_present(:in)
  ensure_type(Enumerable, :in)
end

#validate_attribute(record, attribute_name, value_or_values) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aws/record/validators/inclusion.rb', line 29

def validate_attribute record, attribute_name, value_or_values
  each_value(value_or_values) do |value|

    included = if value.is_a?(Enumerable)
      value.all?{|v| value_included?(v) }
    else
      value_included?(value)
    end

    record.errors.add(attribute_name, message) unless included

  end
end

#value_included?(value) ⇒ Boolean

Returns:

  • (Boolean)

43
44
45
46
47
# File 'lib/aws/record/validators/inclusion.rb', line 43

def value_included? value
  options[:in].respond_to?(:cover?) ?
    options[:in].cover?(value) :
    options[:in].include?(value)
end