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.



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

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

rubocop:disable Style/OptionalBooleanParameter

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) # rubocop:disable Style/OptionalBooleanParameter
  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:



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

def [](bit_index)
  bit_get(bit_index)
end

#bit_get(bit_index) ⇒ EacRubyUtils::Bit

Parameters:

  • bit_index (Integer)

Returns:



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

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



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

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:



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

def to_asm_hex
  ASSEMBLY_HEXADECIMAL_PREFIX + to_hex
end

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



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

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

#to_hexString

Returns:



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

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

#to_iInteger

Returns:

  • (Integer)


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

def to_i
  value
end