Class: MiniDefender::Rules::Uuid

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

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?, #priority, #stops?, #with_message

Constructor Details

#initialize(version = nil) ⇒ Uuid

Returns a new instance of Uuid.

Raises:

  • (ArgumentError)


4
5
6
7
8
# File 'lib/mini_defender/rules/uuid.rb', line 4

def initialize(version = nil)
  raise ArgumentError, 'Expected version to be an integer or nil' unless version.nil? || version.is_a?(Integer)

  @version = version
end

Class Method Details

.make(args) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
# File 'lib/mini_defender/rules/uuid.rb', line 14

def self.make(args)
  raise ArgumentError, 'Expected exactly one or zero argument' unless args.length <= 1

  new(args[0]&.to_i)
end

.signatureObject



10
11
12
# File 'lib/mini_defender/rules/uuid.rb', line 10

def self.signature
  'uuid'
end

Instance Method Details

#coerce(value) ⇒ Object



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

def coerce(value)
  value.downcase
end

#message(attribute, value, validator) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mini_defender/rules/uuid.rb', line 30

def message(attribute, value, validator)
  if @version
    "The value should be a valid UUID v#{@version}."
  else
    'The value should be a valid UUID.'
  end
end

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

Returns:



24
25
26
27
28
# File 'lib/mini_defender/rules/uuid.rb', line 24

def passes?(attribute, value, validator)
  value.is_a?(String) &&
    /^\h{8}-(\h{4}-){3}\h{12}$/i.match?(value) &&
    (@version.nil? || value[14].to_i(16) == @version)
end