Class: Ifconf::MacAddress
- Inherits:
-
Object
- Object
- Ifconf::MacAddress
- Defined in:
- lib/ifconf/mac_address.rb
Overview
Immutable value object representing a validated IEEE 802 MAC address.
Constant Summary collapse
- MAC_PATTERN =
/\A[0-9a-f]{2}(:[0-9a-f]{2}){5}\z/i
Instance Attribute Summary collapse
-
#octets ⇒ Object
readonly
Returns the value of attribute octets.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #broadcast? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(octets) ⇒ MacAddress
constructor
A new instance of MacAddress.
- #locally_administered? ⇒ Boolean
- #multicast? ⇒ Boolean
- #octets_array ⇒ Object
- #to_s ⇒ Object
- #vendor_prefix ⇒ Object
Constructor Details
#initialize(octets) ⇒ MacAddress
Returns a new instance of MacAddress.
24 25 26 27 |
# File 'lib/ifconf/mac_address.rb', line 24 def initialize(octets) @octets = octets freeze end |
Instance Attribute Details
#octets ⇒ Object (readonly)
Returns the value of attribute octets.
8 9 10 |
# File 'lib/ifconf/mac_address.rb', line 8 def octets @octets end |
Class Method Details
.extract_all(text) ⇒ Object
20 21 22 |
# File 'lib/ifconf/mac_address.rb', line 20 def self.extract_all(text) text.scan(Ronin::Support::Text::Patterns::MAC_ADDR).map { |m| parse(m) } end |
.parse(raw) ⇒ Object
10 11 12 13 14 |
# File 'lib/ifconf/mac_address.rb', line 10 def self.parse(raw) raise InvalidMacAddress, raw unless valid?(raw) new(raw.downcase) end |
.valid?(raw) ⇒ Boolean
16 17 18 |
# File 'lib/ifconf/mac_address.rb', line 16 def self.valid?(raw) raw.is_a?(String) && raw.match?(MAC_PATTERN) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
53 54 55 |
# File 'lib/ifconf/mac_address.rb', line 53 def ==(other) other.is_a?(MacAddress) && @octets == other.octets end |
#broadcast? ⇒ Boolean
37 38 39 |
# File 'lib/ifconf/mac_address.rb', line 37 def broadcast? @octets == "ff:ff:ff:ff:ff:ff" end |
#hash ⇒ Object
59 60 61 |
# File 'lib/ifconf/mac_address.rb', line 59 def hash @octets.hash end |
#locally_administered? ⇒ Boolean
45 46 47 |
# File 'lib/ifconf/mac_address.rb', line 45 def locally_administered? Integer(octets_array[0], 16).allbits?(0x02) end |
#multicast? ⇒ Boolean
41 42 43 |
# File 'lib/ifconf/mac_address.rb', line 41 def multicast? Integer(octets_array[0], 16).allbits?(0x01) end |
#octets_array ⇒ Object
29 30 31 |
# File 'lib/ifconf/mac_address.rb', line 29 def octets_array @octets.split(":") end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/ifconf/mac_address.rb', line 49 def to_s @octets end |
#vendor_prefix ⇒ Object
33 34 35 |
# File 'lib/ifconf/mac_address.rb', line 33 def vendor_prefix octets_array[0..2].join(":") end |