Class: DevId::MacAddress

Inherits:
DeviceIdentifier show all
Defined in:
lib/dev-id/mac_address.rb

Overview

Model for a MAC address.

6 bytes.

Constant Summary collapse

DEFAULT_TO_PRETTY_OPTS =

Default options for conversion to ASCII in the pretty method.

{
	:sep => ':',  # typically ":" or "-" or ""
	:upcase => true,
}.freeze

Constants inherited from BinaryAddress

BinaryAddress::DEFAULT_TO_ASCII_OPTS

Instance Attribute Summary

Attributes inherited from BinaryAddress

#raw

Instance Method Summary collapse

Methods inherited from BinaryAddress

#!~, #<=>, #==, #=~, #ascii, #before_validation, #bytes, #bytesize, #byteslice, from_hex, from_raw, #getbyte, #starts_with?, #to_int

Methods inherited from BasicActiveModel

#assign_attributes, #before_validation, #do_before_validation, #initialize, #persisted?

Constructor Details

This class inherits a constructor from DevId::BasicActiveModel

Instance Method Details

#local?Boolean

Whether the MAC address is a local address (as compared to a global address).

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/dev-id/mac_address.rb', line 30

def local?
	return nil  if ! valid?
	#return raw.getbyte(0).to_s(2).rjust(8,'0')[-2] == '1'
	return ((raw.getbyte(0).to_i >> 1) % 2) != 0
end

#multicast?Boolean

Whether the MAC address is a group address (multicast address).

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/dev-id/mac_address.rb', line 23

def multicast?
	return nil  if ! valid?
	return (raw.getbyte(0).to_i % 2) != 0
end

#null?Boolean

Whether the MAC address is the “null” address (6 NULL bytes).

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/dev-id/mac_address.rb', line 39

def null?
	return nil  if ! valid?
	return raw == (?\0.force_encoding( ::Encoding::ASCII_8BIT ) * 6)
end

#oui_rawObject

The OUI (vendor part) as a raw string.



54
55
56
57
# File 'lib/dev-id/mac_address.rb', line 54

def oui_raw
	return nil  if ! valid?
	return raw.byteslice( 0, 3 )
end

#pretty(opts = nil) ⇒ Object

Pretty ASCII representation.



46
47
48
49
50
# File 'lib/dev-id/mac_address.rb', line 46

def pretty( opts = nil )
	return nil  if ! valid?
	opts = DEFAULT_TO_PRETTY_OPTS.merge( opts || {} )
	return ascii( opts )
end

#to_s(opts = nil) ⇒ Object

to_s is an alias for pretty.



61
62
63
# File 'lib/dev-id/mac_address.rb', line 61

def to_s( opts = nil )
	pretty( opts )
end