Class: DevId::BinaryAddress
- Inherits:
-
Base
- Object
- BasicActiveModel
- Base
- DevId::BinaryAddress
- Includes:
- Comparable
- Defined in:
- lib/dev-id/binary_address.rb
Overview
Model for a raw binary address.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_TO_ASCII_OPTS =
Default options for conversion to ASCII in the
asciimethod. { :sep => '', # typically ":" or "-" or "" :upcase => false, }.freeze
Instance Attribute Summary collapse
-
#raw ⇒ Object
The raw address.
Class Method Summary collapse
-
.from_hex(str) ⇒ Object
Construct a new instance from a hex string.
-
.from_raw(str) ⇒ Object
Construct a new instance from a raw string.
Instance Method Summary collapse
- #!~(other) ⇒ Object
-
#<=>(other) ⇒ Object
If
otheris of the same class, compares the raw values. - #==(other) ⇒ Object (also: #eql?)
- #=~(other) ⇒ Object
-
#ascii(opts = nil) ⇒ Object
ASCII representation.
- #before_validation ⇒ Object
- #bytes ⇒ Object
- #bytesize ⇒ Object (also: #length)
- #byteslice(from, len = nil) ⇒ Object (also: #[])
- #getbyte(idx) ⇒ Object
- #starts_with?(other) ⇒ Boolean (also: #start_with?)
-
#to_int ⇒ Object
to_inthelps withinclude?. -
#to_s(opts = nil) ⇒ Object
to_sis an alias forascii.
Methods inherited from BasicActiveModel
#assign_attributes, #do_before_validation, #initialize, #persisted?
Constructor Details
This class inherits a constructor from DevId::BasicActiveModel
Instance Attribute Details
#raw ⇒ Object
The raw address.
13 14 15 |
# File 'lib/dev-id/binary_address.rb', line 13 def raw @raw end |
Class Method Details
.from_hex(str) ⇒ Object
Construct a new instance from a hex string.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dev-id/binary_address.rb', line 61 def self.from_hex( str ) addr = self.new() str = str.to_s.dup str.force_encoding( ::Encoding::ASCII_8BIT ) str.gsub!( /[:\-]/, '' ) raw = str.scanf( '%2X' ) { |x,| x.to_i.chr( ::Encoding::ASCII_8BIT ) }. join( ''.force_encoding( ::Encoding::ASCII_8BIT ) ) addr.raw = raw return addr end |
.from_raw(str) ⇒ Object
Construct a new instance from a raw string.
55 56 57 |
# File 'lib/dev-id/binary_address.rb', line 55 def self.from_raw( str ) addr = self.new( :raw => str ) end |
Instance Method Details
#!~(other) ⇒ Object
142 143 144 |
# File 'lib/dev-id/binary_address.rb', line 142 def !~( other ) return ! (self =~ other) end |
#<=>(other) ⇒ Object
If other is of the same class, compares the raw values. Returns nil unless self and other have a raw address. Returns nil if other is an instance of a different class.
90 91 92 93 94 |
# File 'lib/dev-id/binary_address.rb', line 90 def <=>( other ) return nil unless other.kind_of?( BinaryAddress ) return nil unless (self.raw && other.raw) return self.raw <=> other.raw #OPTIMIZE ? end |
#==(other) ⇒ Object Also known as: eql?
119 120 121 122 123 |
# File 'lib/dev-id/binary_address.rb', line 119 def ==( other ) return super unless other.kind_of?( BinaryAddress ) return false unless (self.raw && other.raw) return self.raw == other.raw end |
#=~(other) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/dev-id/binary_address.rb', line 133 def =~( other ) return super unless other.kind_of?( BinaryAddress ) return false unless (self.raw && other.raw) return (self.bytesize > other.bytesize) ? self .raw.start_with?( other.raw ) : other.raw.start_with?( self .raw ) end |
#ascii(opts = nil) ⇒ Object
ASCII representation.
74 75 76 77 78 |
# File 'lib/dev-id/binary_address.rb', line 74 def ascii( opts = nil ) return nil if ! valid? opts = DEFAULT_TO_ASCII_OPTS.merge( opts || {} ) return self.bytes.map{ |b| (opts[:upcase] ? '%02X' : '%02x') % [b] }.join( opts[:sep] ) end |
#before_validation ⇒ Object
15 16 17 18 19 |
# File 'lib/dev-id/binary_address.rb', line 15 def before_validation if raw raw = raw.to_s.force_encoding( ::Encoding::ASCII_8BIT ) end end |
#bytes ⇒ Object
30 31 32 |
# File 'lib/dev-id/binary_address.rb', line 30 def bytes (raw || '').bytes end |
#bytesize ⇒ Object Also known as: length
34 35 36 |
# File 'lib/dev-id/binary_address.rb', line 34 def bytesize (raw || '').bytesize end |
#byteslice(from, len = nil) ⇒ Object Also known as: []
39 40 41 42 43 44 45 46 |
# File 'lib/dev-id/binary_address.rb', line 39 def byteslice( from, len=nil ) return nil if ! raw if len raw.byteslice( from, len ) else raw.byteslice( from ) # from can be a Fixnum or a Range end end |
#getbyte(idx) ⇒ Object
49 50 51 |
# File 'lib/dev-id/binary_address.rb', line 49 def getbyte( idx ) (raw || '').getbyte( idx ) end |
#starts_with?(other) ⇒ Boolean Also known as: start_with?
126 127 128 129 130 |
# File 'lib/dev-id/binary_address.rb', line 126 def starts_with?( other ) return super unless other.kind_of?( BinaryAddress ) return false unless (self.raw && other.raw) return self.raw.start_with?( other.raw ) end |
#to_int ⇒ Object
to_int helps with include?.
rhnh.net/2009/08/03/range-include-in-ruby-1-9
Note: ActiveSupport (as of Version 3.2.1) messes with Range#include?() (in lib/active_support/core_ext/range/include_range.rb) even though they say “The native Range#include? behavior is untouched.”, so you will have to use Range#cover?() instead.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/dev-id/binary_address.rb', line 107 def to_int v = 0 self.bytes.to_a.reverse.each_with_index { |b,i| vb = b i.times { vb = vb << 8 } v += vb } return v end |
#to_s(opts = nil) ⇒ Object
to_s is an alias for ascii.
82 83 84 |
# File 'lib/dev-id/binary_address.rb', line 82 def to_s( opts = nil ) ascii( opts ) end |