Class: MiniDefender::Rules::CountryCode

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/country_code.rb

Constant Summary collapse

COUNTRIES =
ISO3166::Country.pluck(:alpha2).flatten

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, make, #priority, #stops?, #with_message

Class Method Details

.signatureObject



8
9
10
# File 'lib/mini_defender/rules/country_code.rb', line 8

def self.signature
  'country_code'
end

Instance Method Details

#coerce(value) ⇒ Object



12
13
14
# File 'lib/mini_defender/rules/country_code.rb', line 12

def coerce(value)
  value.strip.upcase
end

#message(attribute, value, validator) ⇒ Object



20
21
22
# File 'lib/mini_defender/rules/country_code.rb', line 20

def message(attribute, value, validator)
  'The value must be a valid ISO3166 country code.'
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



16
17
18
# File 'lib/mini_defender/rules/country_code.rb', line 16

def passes?(attribute, value, validator)
  value.is_a?(String) && COUNTRIES.include?(value.strip.upcase)
end