Class: MiniDefender::Rules::MacAddress

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

Constant Summary collapse

MODES =
%w[any local universal]

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(mode = 'any') ⇒ MacAddress

Returns a new instance of MacAddress.

Raises:

  • (ArgumentError)


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

def initialize(mode = 'any')
  raise ArgumentError, 'Invalid mode' unless MODES.include?(mode)

  @mode = mode
end

Class Method Details

.make(args) ⇒ Object



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

def self.make(args)
  new(args[0] || 'any')
end

.signatureObject



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

def self.signature
  'mac'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



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

def message(attribute, value, validator)
  case @mode
  when 'local'
    "The value must be a locally administrated MAC address."
  when 'universal'
    "The value must be a universally administrated MAC address."
  else
    "The value must be a properly formatted mac address."
  end
end

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

Returns:



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

def passes?(attribute, value, validator)
  clean = value.to_s.gsub(/[-:]/)
  clean.match?(/[0-9A-F]{12}/i) && (
    @mode == 'any' ||
    @mode == 'local' && (clean.hex & 0x020000000000 > 0) ||
    @mode == 'universal' && (clean.hex & 0x020000000000 == 0)
  )
end