Class: Tox::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/tox/binary.rb

Overview

Binary primitive representation.

Direct Known Subclasses

Address, AddressChecksum, Nospam, PublicKey

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Binary

Returns a new instance of Binary.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tox/binary.rb', line 18

def initialize(value)
  unless value.is_a? String
    raise TypeError, "expected value to be a #{String}"
  end

  if value.bytesize == self.class.bytesize
    self.value = value
  elsif value =~ self.class.hex_re
    self.hex_value = value
  else
    raise ArgumentError, 'expected value to be a hex or binary string'
  end
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



16
17
18
# File 'lib/tox/binary.rb', line 16

def value
  @value
end

Class Method Details

.bytesizeObject

Raises:

  • (NotImplementedError)


8
9
10
# File 'lib/tox/binary.rb', line 8

def self.bytesize
  raise NotImplementedError, "#{self}.bytesize"
end

.hex_reObject



12
13
14
# File 'lib/tox/binary.rb', line 12

def self.hex_re
  /\A[\da-fA-F]{#{2 * bytesize}}\z/
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
43
# File 'lib/tox/binary.rb', line 40

def ==(other)
  return false unless self.class == other.class
  value == other.value
end

#hex_value=(value) ⇒ Object (private)



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

def hex_value=(value)
  @value = [value].pack('H*').freeze
end

#inspectObject



36
37
38
# File 'lib/tox/binary.rb', line 36

def inspect
  "#<#{self.class}: \"#{self}\">"
end

#to_sObject



32
33
34
# File 'lib/tox/binary.rb', line 32

def to_s
  @to_s ||= value.unpack('H*').first.upcase.freeze
end