Class: MiniDefender::Rules::ProhibitedUnless

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

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

Constructor Details

#initialize(target, value) ⇒ ProhibitedUnless

Returns a new instance of ProhibitedUnless.

Raises:

  • (ArgumentError)


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

def initialize(target, value)
  raise ArgumentError, 'Target must be a string' unless target.is_a?(String)

  @target = target
  @value = value
end

Class Method Details

.make(args) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/mini_defender/rules/prohibited_unless.rb', line 15

def self.make(args)
  raise ArgumentError, 'Target and expected value are required.' unless args.length == 2

  self.new(args[0], args[1])
end

.signatureObject



11
12
13
# File 'lib/mini_defender/rules/prohibited_unless.rb', line 11

def self.signature
  'prohibited_unless'
end

Instance Method Details

#active?(validator) ⇒ Boolean

Returns:



21
22
23
# File 'lib/mini_defender/rules/prohibited_unless.rb', line 21

def active?(validator)
  validator.data.key?(@target) && validator.data[@target] != @value
end

#message(attribute, value, validator) ⇒ Object



29
30
31
# File 'lib/mini_defender/rules/prohibited_unless.rb', line 29

def message(attribute, value, validator)
  "This field is prohibited."
end

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

Returns:



25
26
27
# File 'lib/mini_defender/rules/prohibited_unless.rb', line 25

def passes?(attribute, value, validator)
  value.blank?
end