eac_ruby_utils 0.88.0 → 0.89.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a3e785240ca74c309c1666c6cabbfd0d4b1533ee2fea2739272efe8925679bf
4
- data.tar.gz: 76439955c6da58c19943e07fca3a8768e539d57b2684bd89d46ff64c64ed7f55
3
+ metadata.gz: e93445e91d5ebd5045f85aacc525da520bfdabf6ca97087296cf98fc37b78af9
4
+ data.tar.gz: 807b4d9838d1b990aa0152bce0c24d3fae4189b92ad07575b6ea26ec9dd7c3a6
5
5
  SHA512:
6
- metadata.gz: c9700725a581ac9903843337f2ee962ec8f932e5f2aabfa448affca8ad306096857400273d6571e5c30886cbb17b120e7f5836bddbbcfcc3dc2ddba583473ee7
7
- data.tar.gz: 1d11cbb29b0835442f20e05a326e17162a9ad1d9753af22e9f1e36ccfc956c87973e9a72bfa2880cf54d3f4257cb3909e97d08c511cc656cb8979f5580a50643
6
+ metadata.gz: f33fdf6f0aad6e64d072074a396a58c51b9116827aa91847025a4c91cc880e6af8311b34b3dbfdbd55b15fb9612cdba211da853895b4b2182b07229e82c84bd4
7
+ data.tar.gz: 03c783a9676f370fe1b4d629fdcc227d759c4e7fe37335c8fe15c15e63234f836d8b2a6bf95e389d948210f6cd9880ce261814e8a52644263e9ac7b4c6b144ed
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/module/compare_by'
4
+
5
+ module EacRubyUtils
6
+ class Bit
7
+ VALID_VALUES = [0, 1].freeze
8
+
9
+ class << self
10
+ def assert(obj)
11
+ return obj if obj.is_a?(self)
12
+
13
+ new(obj.to_i)
14
+ end
15
+
16
+ def valid_integer?(value)
17
+ value.is_a?(::Integer) && VALID_VALUES.include?(value)
18
+ end
19
+
20
+ def validate_integer(value)
21
+ return value if valid_integer?(value)
22
+
23
+ raise(::ArgumentError, "Invalid bit value: #{value} (Valid: #{VALID_VALUES})")
24
+ end
25
+ end
26
+
27
+ attr_reader :value
28
+ compare_by :value
29
+ delegate :to_s, :zero?, to: :value
30
+
31
+ # @param value [Integer]
32
+ def initialize(value)
33
+ @value = self.class.validate_integer(value)
34
+ end
35
+
36
+ # @return [Boolean]
37
+ def one?
38
+ !zero?
39
+ end
40
+
41
+ # @return [Integer]
42
+ def to_i
43
+ value
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/bit'
4
+ require 'eac_ruby_utils/byte'
5
+ require 'eac_ruby_utils/byte_array'
6
+ require 'eac_ruby_utils/patches/module/compare_by'
7
+
8
+ module EacRubyUtils
9
+ class BitArray
10
+ class << self
11
+ def assert(obj)
12
+ return obj if obj.is_a?(self)
13
+ return new(obj) if obj.is_a?(::Enumerable)
14
+
15
+ raise "Could not convert #{obj} to #{self}"
16
+ end
17
+ end
18
+
19
+ delegate :each, :each_with_index, :to_a, :size, :count, :length, :[], :fetch, to: :values_array
20
+ compare_by :values_array
21
+
22
+ def initialize(values = [])
23
+ values.each { |value| push(value) }
24
+ end
25
+
26
+ def <<(value)
27
+ push(value)
28
+ end
29
+
30
+ # @param value [EacRubyUtils::Bit]
31
+ # @return [EacRubyUtils::Bit]
32
+ def push(value)
33
+ values_array.push(::EacRubyUtils::Bit.assert(value))
34
+ end
35
+
36
+ # @return [EacRubyUtils::BitArray]
37
+ def reverse
38
+ self.class.new(values_array.reverse)
39
+ end
40
+
41
+ # @param big_endian [Boolean]
42
+ # @return [EacRubyUtils::ByteArray]
43
+ def to_byte_array(big_endian = false)
44
+ unless count.modulo(::EacRubyUtils::Byte::BIT_COUNT).zero?
45
+ raise 'Bits returned is not multile of 8'
46
+ end
47
+
48
+ byte_bits_enumerator.each_with_object(::EacRubyUtils::ByteArray.new) do |e, a|
49
+ a << ::EacRubyUtils::Byte.from_bit_array(e, big_endian)
50
+ end
51
+ end
52
+
53
+ # @return [Array<Integer>]
54
+ def to_int_array
55
+ values_array.map(&:to_i)
56
+ end
57
+
58
+ private
59
+
60
+ def byte_bits_enumerator
61
+ ::Enumerator.new do |y|
62
+ offset = 0
63
+ while offset < values_array.count
64
+ y.yield(values_array.slice(offset, ::EacRubyUtils::Byte::BIT_COUNT))
65
+ offset += ::EacRubyUtils::Byte::BIT_COUNT
66
+ end
67
+ end
68
+ end
69
+
70
+ def values_array
71
+ @values_array ||= []
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/bit'
4
+ require 'eac_ruby_utils/bit_array'
5
+ require 'eac_ruby_utils/patches/module/compare_by'
6
+
7
+ module EacRubyUtils
8
+ class Byte
9
+ BIT_COUNT = 8
10
+ BIT_INDEX_RANGE = (0..7).freeze
11
+ VALUE_RANGE = (0..255).freeze
12
+
13
+ class << self
14
+ def assert(obj)
15
+ return obj if obj.is_a?(self)
16
+
17
+ new(obj.to_i)
18
+ end
19
+
20
+ def from_bit_array(bit_array, big_endian = false)
21
+ bit_array = ::EacRubyUtils::BitArray.assert(bit_array)
22
+ raise ::ArgumentError, "Wrong bit array size: #{bit_array.size}" if
23
+ bit_array.size != BIT_COUNT
24
+
25
+ bit_array = bit_array.reverse if big_endian
26
+ bit_array.each_with_index.inject(new(0)) do |a, e|
27
+ a.bit_set(e[1], e[0])
28
+ end
29
+ end
30
+
31
+ def valid_bit_index?(value)
32
+ value.is_a?(::Integer) && BIT_INDEX_RANGE.include?(value)
33
+ end
34
+
35
+ def validate_bit_index(value)
36
+ return value if valid_bit_index?(value)
37
+
38
+ raise(::ArgumentError, "Invalid bit index: #{value} (Range: #{BIT_INDEX_RANGE})")
39
+ end
40
+
41
+ def valid_value?(value)
42
+ value.is_a?(::Integer) && VALUE_RANGE.include?(value)
43
+ end
44
+
45
+ def validate_value(value)
46
+ return value if valid_value?(value)
47
+
48
+ raise(::ArgumentError, "Invalid byte value: #{value} (Range: #{VALUE_RANGE})")
49
+ end
50
+ end
51
+
52
+ attr_reader :value
53
+ compare_by :value
54
+
55
+ def initialize(value)
56
+ self.value = value
57
+ end
58
+
59
+ # @param bit_index [Integer]
60
+ # @return [EacRubyUtils::Bit]
61
+ def [](bit_index)
62
+ bit_get(bit_index)
63
+ end
64
+
65
+ # @param bit_index [Integer]
66
+ # @return [EacRubyUtils::Bit]
67
+ def bit_get(bit_index)
68
+ self.class.validate_bit_index(bit_index)
69
+
70
+ ::EacRubyUtils::Bit.new((value & (1 << bit_index)) >> bit_index)
71
+ end
72
+
73
+ def bit_set(bit_index, bit_value)
74
+ self.class.validate_bit_index(bit_index)
75
+ bit = ::EacRubyUtils::Bit.assert(bit_value)
76
+ mask = (1 << bit_index)
77
+ self.class.new(bit.zero? ? value & ~mask : value | mask)
78
+ end
79
+
80
+ def to_bit_array
81
+ BIT_INDEX_RANGE.map { |bit_index| self[bit_index] }
82
+ end
83
+
84
+ # @return [Integer]
85
+ def to_i
86
+ value
87
+ end
88
+
89
+ private
90
+
91
+ attr_writer :value
92
+ end
93
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/byte'
4
+ require 'eac_ruby_utils/patches/module/compare_by'
5
+
6
+ module EacRubyUtils
7
+ class ByteArray
8
+ delegate :to_a, :size, :count, :length, :[], :fetch, to: :values_array
9
+ compare_by :values_array
10
+
11
+ def initialize(values = [])
12
+ values.each { |value| push(value) }
13
+ end
14
+
15
+ def <<(value)
16
+ push(value)
17
+ end
18
+
19
+ # @param value [EacRubyUtils::Byte]
20
+ # @return [EacRubyUtils::Byte]
21
+ def push(value)
22
+ values_array.push(::EacRubyUtils::Byte.assert(value))
23
+ end
24
+
25
+ # @return [Array<Integer>]
26
+ def to_int_array
27
+ values_array.map(&:to_i)
28
+ end
29
+
30
+ private
31
+
32
+ def values_array
33
+ @values_array ||= []
34
+ end
35
+ end
36
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.88.0'
4
+ VERSION = '0.89.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_ruby_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.88.0
4
+ version: 0.89.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-03 00:00:00.000000000 Z
11
+ date: 2022-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -102,9 +102,13 @@ files:
102
102
  - lib/eac_ruby_utils.rb
103
103
  - lib/eac_ruby_utils/abstract_methods.rb
104
104
  - lib/eac_ruby_utils/arguments_consumer.rb
105
+ - lib/eac_ruby_utils/bit.rb
106
+ - lib/eac_ruby_utils/bit_array.rb
105
107
  - lib/eac_ruby_utils/blank_not_blank.rb
106
108
  - lib/eac_ruby_utils/boolean.rb
107
109
  - lib/eac_ruby_utils/by_reference.rb
110
+ - lib/eac_ruby_utils/byte.rb
111
+ - lib/eac_ruby_utils/byte_array.rb
108
112
  - lib/eac_ruby_utils/common_concern.rb
109
113
  - lib/eac_ruby_utils/common_concern/class_setup.rb
110
114
  - lib/eac_ruby_utils/common_concern/module_setup.rb