Class: MiniDefender::Rules::NotStartingWith

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/not_starting_with.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(fragments) ⇒ NotStartingWith

Returns a new instance of NotStartingWith.



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

def initialize(fragments)
  unless fragments.is_a?(Array) && !fragments.empty? && fragments.all? { |f| f.is_a?(String) }
    raise ArgumentError, 'Expected an array of strings.'
  end

  @fragments = fragments
end

Class Method Details

.make(args) ⇒ Object



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

def self.make(args)
  new(args)
end

.signatureObject



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

def self.signature
  'not_starting_with'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



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

def message(attribute, value, validator)
  if @fragments.length == 1
    "The value should not start with #{@fragments[0]}."
  else
    "The value should not start with one of the following #{@fragments.join(', ')}."
  end
end

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

Returns:



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

def passes?(attribute, value, validator)
  @fragments.any? { |f| !value.to_s.start_with?(f) }
end