Class: HWAddr

Inherits:
Object
  • Object
show all
Defined in:
lib/hwaddr.rb,
lib/hwaddr/database.rb

Overview

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                  Version 2, December 2004

          DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Direct Known Subclasses

Database::Company::Range

Defined Under Namespace

Classes: Database

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ HWAddr

Returns a new instance of HWAddr.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hwaddr.rb', line 18

def initialize (value)
	@string = if value.is_a?(Array)
		if value.length > 6
			raise ArgumentError, "#{value.inspect} is too big for a MAC address"
		end

		value.map { |b| "%02x" % b }.join(':')
	elsif value.is_a?(Integer)
		array = []

		6.times {
			array << value & 0xff
			value >>= 8
		}

		array.map { |b| "%02x" % b }.join(':')
	else
		value.to_s
	end

	@string.tr! '-', ':'

	unless HWAddr.valid?(@string)
		raise ArgumentError, "#{value} isn't an usable MAC address"
	end
	
	@string.downcase!
end

Class Method Details

.valid?(text) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/hwaddr.rb', line 14

def self.valid? (text)
	text =~ /^\h\h:\h\h:\h\h(?::\h\h:\h\h:\h\h)?$/
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



51
52
53
# File 'lib/hwaddr.rb', line 51

def == (other)
	to_s == other.to_s
end

#=~(other) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/hwaddr.rb', line 57

def =~ (other)
	if group?
		to_s == other.to_s[0, 8]
	else
		to_s == other.to_s
	end
end

#broadcast?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/hwaddr.rb', line 69

def broadcast?
	to_s == 'ff:ff:ff:ff:ff:ff'
end

#group?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/hwaddr.rb', line 65

def group?
	to_s.length == 8
end

#hashObject



47
48
49
# File 'lib/hwaddr.rb', line 47

def hash
	to_s.hash
end

#inspectObject



81
82
83
# File 'lib/hwaddr.rb', line 81

def inspect
	"#<HWAddr: #{to_s}>"
end

#productorObject



73
74
75
# File 'lib/hwaddr.rb', line 73

def productor
	Database[to_s]
end

#to_sObject



77
78
79
# File 'lib/hwaddr.rb', line 77

def to_s
	@string
end