Class: EacRubyUtils::Byte

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

Constant Summary collapse

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.



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

def initialize(value)
  self.value = value
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.assert(obj) ⇒ Object



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

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)


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

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:



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

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

.valid_value?(value) ⇒ Boolean

Returns:



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

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

.validate_bit_index(value) ⇒ Object

Raises:

  • (::ArgumentError)


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

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)


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

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:



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

def [](bit_index)
  bit_get(bit_index)
end

#bit_get(bit_index) ⇒ EacRubyUtils::Bit

Parameters:

  • bit_index (Integer)

Returns:



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

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



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

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_bit_arrayObject



80
81
82
# File 'lib/eac_ruby_utils/byte.rb', line 80

def to_bit_array
  BIT_INDEX_RANGE.map { |bit_index| self[bit_index] }
end

#to_iInteger

Returns:

  • (Integer)


85
86
87
# File 'lib/eac_ruby_utils/byte.rb', line 85

def to_i
  value
end