Class: MiniDefender::Rules::InField

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

Returns a new instance of InField.

Raises:

  • (ArgumentError)


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

def initialize(field)
  raise ArgumentError, 'Field must be a string.' unless field.is_a?(String)

  @field = field
end

Class Method Details

.make(args) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.make(args)
  raise ArgumentError, 'Expected one argument, target field name.' unless args.length == 1

  new(args[0])
end

.signatureObject



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

def self.signature
  'in_field'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/mini_defender/rules/in_field.rb', line 30

def message(attribute, value, validator)
  case @field_value
  when nil
    "The field (#{@field}) is missing."
  when Array
    "The field (#{@field}) must be an array."
  else
    "The value must be one of the values found in #{@field}."
  end
end

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

Returns:



20
21
22
23
24
25
26
27
28
# File 'lib/mini_defender/rules/in_field.rb', line 20

def passes?(attribute, value, validator)
  @field_value = nil
  return false unless validator.data.key(@field)

  @field_value = validator.data[@field]
  return false unless field.is_a?(Array)

  @field_value.include?(value)
end