eac_ruby_utils 0.88.0 → 0.91.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: 67479e6645bae216dae24b09ff55f7cb5b12d803113963a3990c4c9215b47827
4
+ data.tar.gz: d9e542d4e1ba6d39623ce2981da1d3a618946ae483a3822829e3bac92a2fb9cd
5
5
  SHA512:
6
- metadata.gz: c9700725a581ac9903843337f2ee962ec8f932e5f2aabfa448affca8ad306096857400273d6571e5c30886cbb17b120e7f5836bddbbcfcc3dc2ddba583473ee7
7
- data.tar.gz: 1d11cbb29b0835442f20e05a326e17162a9ad1d9753af22e9f1e36ccfc956c87973e9a72bfa2880cf54d3f4257cb3909e97d08c511cc656cb8979f5580a50643
6
+ metadata.gz: 98b6e459e408b63bdf208d9376829e40dac7bac0e062f872b9313440ccec5e0ac35ba11ff339227578c540a6a8463e8ab531d17f77a3ef4bdcd7874352f1a892
7
+ data.tar.gz: 7da0fb8b6fba05183cdb977ab15fd0e8c93c7d3b3b491e0bcb67e66fdd904def1cabf796de0a64a65526f0a0e427bc21455a9d486ada5e711325f43fe45d6a21
@@ -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,78 @@
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
+ def push_array(other_bit_array)
31
+ @values_array += other_bit_array.values_array
32
+ end
33
+
34
+ # @param value [EacRubyUtils::Bit]
35
+ # @return [EacRubyUtils::Bit]
36
+ def push(value)
37
+ values_array.push(::EacRubyUtils::Bit.assert(value))
38
+ end
39
+
40
+ # @return [EacRubyUtils::BitArray]
41
+ def reverse
42
+ self.class.new(values_array.reverse)
43
+ end
44
+
45
+ # @param big_endian [Boolean]
46
+ # @return [EacRubyUtils::ByteArray]
47
+ def to_byte_array(big_endian = false)
48
+ unless count.modulo(::EacRubyUtils::Byte::BIT_COUNT).zero?
49
+ raise 'Bits returned is not multile of 8'
50
+ end
51
+
52
+ byte_bits_enumerator.each_with_object(::EacRubyUtils::ByteArray.new) do |e, a|
53
+ a << ::EacRubyUtils::Byte.from_bit_array(e, big_endian)
54
+ end
55
+ end
56
+
57
+ # @return [Array<Integer>]
58
+ def to_int_array
59
+ values_array.map(&:to_i)
60
+ end
61
+
62
+ private
63
+
64
+ def byte_bits_enumerator
65
+ ::Enumerator.new do |y|
66
+ offset = 0
67
+ while offset < values_array.count
68
+ y.yield(values_array.slice(offset, ::EacRubyUtils::Byte::BIT_COUNT))
69
+ offset += ::EacRubyUtils::Byte::BIT_COUNT
70
+ end
71
+ end
72
+ end
73
+
74
+ def values_array
75
+ @values_array ||= []
76
+ end
77
+ end
78
+ 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
@@ -11,4 +11,15 @@ class Object
11
11
  "Argument \"#{argument_name}\" is not a #{klass}" \
12
12
  "(Actual class: #{self.class}, actual value: #{self})"
13
13
  end
14
+
15
+ # Raises a ArgumentError if +self.count+ is not equal to +count+.
16
+ #
17
+ # @return +self+
18
+ def assert_count(count, argument_name = 'unknown_argument_name')
19
+ return self if self.count == count
20
+
21
+ raise ::ArgumentError,
22
+ "Argument \"#{argument_name}\" has wrong elements count" \
23
+ "(Actual: #{self.count}, Required: #{count})"
24
+ end
14
25
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ class Pathname
6
+ # A .glob that raises a ::RuntimeError if +self+ is not a directory.
7
+ # @return [Pathname]
8
+ def reqdir_glob(*args)
9
+ raise ::RuntimeError, "\"#{self}\" is not a directory" unless directory?
10
+
11
+ glob(*args)
12
+ end
13
+ 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.91.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.91.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-22 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
@@ -211,6 +215,7 @@ files:
211
215
  - lib/eac_ruby_utils/patches/pathname/mkpath_s.rb
212
216
  - lib/eac_ruby_utils/patches/pathname/parent_n.rb
213
217
  - lib/eac_ruby_utils/patches/pathname/readlink_r.rb
218
+ - lib/eac_ruby_utils/patches/pathname/reqdir_glob.rb
214
219
  - lib/eac_ruby_utils/patches/regexp.rb
215
220
  - lib/eac_ruby_utils/patches/regexp/if_match.rb
216
221
  - lib/eac_ruby_utils/patches/regexp/to_parser.rb