Class: MiniDefender::Rules::NotRegex

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

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

Constructor Details

#initialize(pattern) ⇒ NotRegex

Returns a new instance of NotRegex.

Raises:

  • (ArgumentError)


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

def initialize(pattern)
  raise ArgumentError, 'Expected a Regexp instance.' unless pattern.is_a?(Regexp)

  @pattern = pattern
end

Class Method Details

.make(args) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.make(args)
  raise ArgumentError, 'Expected a pattern as input.' unless args.length > 0

  new(Regexp.compile(args.join(',')))
end

.signatureObject



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

def self.signature
  'not_regex'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



24
25
26
# File 'lib/mini_defender/rules/not_regex.rb', line 24

def message(attribute, value, validator)
  "The value must not match #{@pattern.to_s}."
end

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

Returns:



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

def passes?(attribute, value, validator)
  !value.to_s.match?(@pattern)
end