Class: EacRubyUtils::Byte

Inherits:
Object show all
Defined in:
lib/eac_ruby_utils/byte.rb

Constant Summary collapse

ASSEMBLY_HEXADECIMAL_PREFIX =
'$'
BIT_COUNT =
8
BIT_INDEX_RANGE =
(0..7).freeze
VALUE_RANGE =
(0..255).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Byte

Returns a new instance of Byte.



56
57
58
# File 'lib/eac_ruby_utils/byte.rb', line 56

def initialize(value)
  self.value = value
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



53
54
55
# File 'lib/eac_ruby_utils/byte.rb', line 53

def value
  @value
end

Class Method Details

.assert(obj) ⇒ Object



15
16
17
18
19
# File 'lib/eac_ruby_utils/byte.rb', line 15

def assert(obj)
  return obj if obj.is_a?(self)

  new(obj.to_i)
end

.from_bit_array(bit_array, big_endian = false) ⇒ Object

Raises:

  • (::ArgumentError)


21
22
23
24
25
26
27
28
29
30
# File 'lib/eac_ruby_utils/byte.rb', line 21

def from_bit_array(bit_array, big_endian = false)
  bit_array = ::EacRubyUtils::BitArray.assert(bit_array)
  raise ::ArgumentError, "Wrong bit array size: #{bit_array.size}" if
  bit_array.size != BIT_COUNT

  bit_array = bit_array.reverse if big_endian
  bit_array.each_with_index.inject(new(0)) do |a, e|
    a.bit_set(e[1], e[0])
  end
end

.valid_bit_index?(value) ⇒ Boolean

Returns:



32
33
34
# File 'lib/eac_ruby_utils/byte.rb', line 32

def valid_bit_index?(value)
  value.is_a?(::Integer) && BIT_INDEX_RANGE.include?(value)
end

.valid_value?(value) ⇒ Boolean

Returns:



42
43
44
# File 'lib/eac_ruby_utils/byte.rb', line 42

def valid_value?(value)
  value.is_a?(::Integer) && VALUE_RANGE.include?(value)
end

.validate_bit_index(value) ⇒ Object

Raises:

  • (::ArgumentError)


36
37
38
39
40
# File 'lib/eac_ruby_utils/byte.rb', line 36

def validate_bit_index(value)
  return value if valid_bit_index?(value)

  raise(::ArgumentError, "Invalid bit index: #{value} (Range: #{BIT_INDEX_RANGE})")
end

.validate_value(value) ⇒ Object

Raises:

  • (::ArgumentError)


46
47
48
49
50
# File 'lib/eac_ruby_utils/byte.rb', line 46

def validate_value(value)
  return value if valid_value?(value)

  raise(::ArgumentError, "Invalid byte value: #{value} (Range: #{VALUE_RANGE})")
end

Instance Method Details

#[](bit_index) ⇒ EacRubyUtils::Bit

Parameters:

  • bit_index (Integer)

Returns:



62
63
64
# File 'lib/eac_ruby_utils/byte.rb', line 62

def [](bit_index)
  bit_get(bit_index)
end

#bit_get(bit_index) ⇒ EacRubyUtils::Bit

Parameters:

  • bit_index (Integer)

Returns:



68
69
70
71
72
# File 'lib/eac_ruby_utils/byte.rb', line 68

def bit_get(bit_index)
  self.class.validate_bit_index(bit_index)

  ::EacRubyUtils::Bit.new((value & (1 << bit_index)) >> bit_index)
end

#bit_set(bit_index, bit_value) ⇒ Object



74
75
76
77
78
79
# File 'lib/eac_ruby_utils/byte.rb', line 74

def bit_set(bit_index, bit_value)
  self.class.validate_bit_index(bit_index)
  bit = ::EacRubyUtils::Bit.assert(bit_value)
  mask = (1 << bit_index)
  self.class.new(bit.zero? ? value & ~mask : value | mask)
end

#to_asm_hexString

Returns:



92
93
94
# File 'lib/eac_ruby_utils/byte.rb', line 92

def to_asm_hex
  ASSEMBLY_HEXADECIMAL_PREFIX + to_hex
end

#to_bit_array(range = BIT_INDEX_RANGE) ⇒ EacRubyUtils::BitArray



82
83
84
# File 'lib/eac_ruby_utils/byte.rb', line 82

def to_bit_array(range = BIT_INDEX_RANGE)
  ::EacRubyUtils::BitArray.new(range.map { |bit_index| self[bit_index] })
end

#to_hexString

Returns:



97
98
99
# File 'lib/eac_ruby_utils/byte.rb', line 97

def to_hex
  value.to_s(16).upcase.rjust(2, '0')
end

#to_iInteger

Returns:

  • (Integer)


87
88
89
# File 'lib/eac_ruby_utils/byte.rb', line 87

def to_i
  value
end