avm-tools 0.114.1 → 0.116.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/avm/tools/version.rb +1 -1
- data/sub/avm/lib/avm/instances/base/auto_values/database.rb +2 -2
- data/sub/avm/lib/avm/instances/entry_keys.rb +2 -1
- data/sub/avm/lib/avm/self/instance.rb +0 -1
- data/sub/avm/lib/avm/sources/base/instance.rb +28 -0
- data/sub/avm/lib/avm/version.rb +1 -1
- data/sub/avm-eac_postgresql_base0/lib/avm/eac_postgresql_base0/instance/assert.rb +77 -0
- data/sub/avm-eac_postgresql_base0/lib/avm/eac_postgresql_base0/instance.rb +23 -12
- data/sub/avm-eac_postgresql_base0/lib/avm/eac_postgresql_base0/version.rb +1 -1
- data/sub/avm-eac_rails_base0/avm-eac_rails_base0.gemspec +1 -1
- data/sub/avm-eac_rails_base0/lib/avm/eac_rails_base0/deploy.rb +1 -1
- data/sub/avm-eac_rails_base0/lib/avm/eac_rails_base0/version.rb +1 -1
- data/sub/avm-eac_rails_base1/avm-eac_rails_base1.gemspec +1 -1
- data/sub/avm-eac_rails_base1/lib/avm/eac_rails_base1/runner/tasks_scheduler/systemd_unit.rb +46 -0
- data/sub/avm-eac_rails_base1/lib/avm/eac_rails_base1/runner/tasks_scheduler.rb +17 -0
- data/sub/avm-eac_rails_base1/lib/avm/eac_rails_base1/systemd_unit/service.rb +70 -0
- data/sub/avm-eac_rails_base1/lib/avm/eac_rails_base1/systemd_unit/tasks_scheduler_command.rb +29 -0
- data/sub/avm-eac_rails_base1/lib/avm/eac_rails_base1/systemd_unit.rb +57 -0
- data/sub/avm-eac_rails_base1/lib/avm/eac_rails_base1/version.rb +1 -1
- data/sub/avm-eac_rails_base1/template/avm/eac_rails_base1/systemd_unit/tasks_scheduler.service +12 -0
- data/sub/avm-eac_rails_base1/template/avm/eac_rails_base1/systemd_unit/tasks_scheduler_command.sh +6 -0
- data/sub/avm-eac_redmine_base0/avm-eac_redmine_base0.gemspec +2 -1
- data/sub/avm-eac_redmine_base0/lib/avm/eac_redmine_base0/data_unit.rb +6 -10
- data/sub/avm-eac_redmine_base0/lib/avm/eac_redmine_base0/instance.rb +20 -0
- data/sub/avm-eac_redmine_base0/lib/avm/eac_redmine_base0/rest_api.rb +11 -0
- data/sub/avm-eac_redmine_base0/lib/avm/eac_redmine_base0/version.rb +1 -1
- data/sub/avm-eac_redmine_base0/template/avm/eac_redmine_base0/deploy/config/install.sh.template +3 -3
- data/sub/avm-eac_ubuntu_base0/lib/avm/eac_ubuntu_base0/instances/base.rb +6 -0
- data/sub/avm-eac_ubuntu_base0/lib/avm/eac_ubuntu_base0/version.rb +1 -1
- data/sub/avm-eac_webapp_base0/avm-eac_webapp_base0.gemspec +1 -1
- data/sub/avm-eac_webapp_base0/lib/avm/eac_webapp_base0/instance.rb +7 -0
- data/sub/avm-eac_webapp_base0/lib/avm/eac_webapp_base0/version.rb +1 -1
- data/sub/eac_cli/lib/eac_cli/runner/context.rb +4 -0
- data/sub/eac_cli/lib/eac_cli/runner/instance_methods.rb +10 -0
- data/sub/eac_cli/lib/eac_cli/runner_with/subcommands.rb +1 -1
- data/sub/eac_cli/lib/eac_cli/version.rb +1 -1
- data/sub/eac_fs/lib/eac_fs/patches/object/fs_cache.rb +6 -1
- data/sub/eac_fs/lib/eac_fs/version.rb +1 -1
- data/sub/eac_rest/Gemfile +5 -0
- data/sub/eac_rest/eac_rest.gemspec +18 -0
- data/sub/eac_rest/lib/eac_rest/api.rb +39 -0
- data/sub/eac_rest/lib/eac_rest/entity.rb +16 -0
- data/sub/eac_rest/lib/eac_rest/request.rb +74 -0
- data/sub/eac_rest/lib/eac_rest/response.rb +72 -0
- data/sub/eac_rest/lib/eac_rest/version.rb +5 -0
- data/sub/eac_rest/lib/eac_rest.rb +7 -0
- data/sub/eac_rest/spec/rubocop_spec.rb +3 -0
- data/sub/eac_rest/spec/spec_helper.rb +4 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/bit.rb +46 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/bit_array.rb +81 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/byte.rb +105 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/byte_array.rb +36 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/envs/process.rb +12 -4
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/patches/object/asserts.rb +11 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/patches/pathname/basename_noext.rb +10 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/patches/pathname/reqdir_glob.rb +13 -0
- data/sub/eac_ruby_utils/lib/eac_ruby_utils/version.rb +1 -1
- data/sub/eac_ruby_utils/spec/lib/eac_ruby_utils/bit_array_spec.rb +22 -0
- data/sub/eac_ruby_utils/spec/lib/eac_ruby_utils/byte_spec.rb +54 -0
- data/sub/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/pathname/basename_noext_spec.rb +26 -0
- metadata +41 -12
@@ -10,6 +10,12 @@ module Avm
|
|
10
10
|
def docker_image_class
|
11
11
|
::Avm::EacUbuntuBase0::DockerImage
|
12
12
|
end
|
13
|
+
|
14
|
+
def file_sudo_write(path, content)
|
15
|
+
::EacRubyUtils::Envs.local.command('echo', content).pipe(
|
16
|
+
host_env.command('sudo', 'tee', path)
|
17
|
+
).execute!
|
18
|
+
end
|
13
19
|
end
|
14
20
|
end
|
15
21
|
end
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.add_dependency 'avm', '~> 0.1'
|
16
16
|
s.add_dependency 'avm-eac_generic_base0', '~> 0.1'
|
17
17
|
s.add_dependency 'avm-eac_postgresql_base0', '~> 0.1'
|
18
|
-
s.add_dependency 'avm-eac_ubuntu_base0', '~> 0.
|
18
|
+
s.add_dependency 'avm-eac_ubuntu_base0', '~> 0.3'
|
19
19
|
s.add_dependency 'eac_ruby_utils', '~> 0.68'
|
20
20
|
|
21
21
|
s.add_development_dependency 'eac_ruby_gem_support', '~> 0.2'
|
@@ -5,12 +5,14 @@ require 'avm/eac_postgresql_base0/instance_with'
|
|
5
5
|
require 'avm/data/instance/files_unit'
|
6
6
|
require 'avm/data/instance/package'
|
7
7
|
require 'avm/eac_webapp_base0/deploy/file_unit'
|
8
|
+
require 'avm/eac_ubuntu_base0/instances/base'
|
8
9
|
|
9
10
|
module Avm
|
10
11
|
module EacWebappBase0
|
11
12
|
class Instance < ::Avm::Instances::Base
|
12
13
|
require_sub __FILE__
|
13
14
|
include ::Avm::EacPostgresqlBase0::InstanceWith
|
15
|
+
enable_simple_cache
|
14
16
|
|
15
17
|
FILES_UNITS = [].freeze
|
16
18
|
|
@@ -45,6 +47,11 @@ module Avm
|
|
45
47
|
|
46
48
|
private
|
47
49
|
|
50
|
+
# @return [Avm::EacUbuntuBase0::Instances::Base]
|
51
|
+
def platform_instance_uncached
|
52
|
+
::Avm::EacUbuntuBase0::Instances::Base.by_id(id)
|
53
|
+
end
|
54
|
+
|
48
55
|
def files_units
|
49
56
|
self.class.const_get('FILES_UNITS').transform_values do |fs_path_subpath|
|
50
57
|
::Avm::Data::Instance::FilesUnit.new(self, fs_path_subpath)
|
@@ -23,6 +23,10 @@ module EacCli
|
|
23
23
|
raise ::NameError, "No method \"#{method_name}\" found in #{runner} or in its ancestors"
|
24
24
|
end
|
25
25
|
|
26
|
+
def respond_to_call?((method_name))
|
27
|
+
parent.respond_to?(:runner_context) && parent.runner_context.respond_to_call?(method_name)
|
28
|
+
end
|
29
|
+
|
26
30
|
protected
|
27
31
|
|
28
32
|
def parent_call(method_name, *args)
|
@@ -37,6 +37,16 @@ module EacCli
|
|
37
37
|
def program_name
|
38
38
|
runner_context.if_present(&:program_name) || $PROGRAM_NAME
|
39
39
|
end
|
40
|
+
|
41
|
+
def respond_to_missing?(method, include_all = false)
|
42
|
+
runner_context.respond_to_call?(method) || super
|
43
|
+
end
|
44
|
+
|
45
|
+
def method_missing(method, *args, &block)
|
46
|
+
return super unless runner_context.respond_to_call?(method)
|
47
|
+
|
48
|
+
runner_context.call(method, *args, &block)
|
49
|
+
end
|
40
50
|
end
|
41
51
|
end
|
42
52
|
end
|
@@ -95,7 +95,7 @@ module EacCli
|
|
95
95
|
raise(::EacCli::Parser::Error.new(
|
96
96
|
self.class.runner_definition, runner_context.argv,
|
97
97
|
"Subcommand \"#{subcommand_name}\" not found " \
|
98
|
-
"(Available: #{available_subcommands.keys}"
|
98
|
+
"(Available: #{available_subcommands.keys})"
|
99
99
|
))
|
100
100
|
end
|
101
101
|
|
@@ -10,7 +10,12 @@ class Object
|
|
10
10
|
def fs_#{type}
|
11
11
|
oid = fs_object_id_by_type(:'#{type}')
|
12
12
|
oid = [oid.to_s] unless oid.is_a?(::Enumerable)
|
13
|
-
oid.inject(
|
13
|
+
oid.inject(fs_#{type}_parent) { |a, e| a.child(e.to_s) }
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [EacFs::StorageTree]
|
17
|
+
def fs_#{type}_parent
|
18
|
+
self.class.fs_#{type}
|
14
19
|
end
|
15
20
|
CODE
|
16
21
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
4
|
+
|
5
|
+
require 'eac_rest/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'eac_rest'
|
9
|
+
s.version = EacRest::VERSION
|
10
|
+
s.authors = ['Eduardo H. Bogoni']
|
11
|
+
s.summary = 'A REST helper for Ruby.'
|
12
|
+
|
13
|
+
s.files = Dir['{lib}/**/*']
|
14
|
+
|
15
|
+
s.add_dependency 'eac_ruby_utils', '~> 0.70'
|
16
|
+
|
17
|
+
s.add_development_dependency 'eac_ruby_gem_support', '~> 0.3'
|
18
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_rest/request'
|
4
|
+
require 'eac_ruby_utils/core_ext'
|
5
|
+
|
6
|
+
module EacRest
|
7
|
+
# Abstract methods
|
8
|
+
# * self.issue_get_url_suffix(provider_issue_id)
|
9
|
+
class Api
|
10
|
+
require_sub __FILE__, include_modules: true
|
11
|
+
common_constructor :root_url, :username, :password, default: [nil, nil]
|
12
|
+
|
13
|
+
def request(service_url_suffix, headers = {}, &body_data_proc)
|
14
|
+
r = ::EacRest::Request.new(build_service_url(service_url_suffix),
|
15
|
+
body_data_proc)
|
16
|
+
headers.each { |name, value| r = r.header(name, value) }
|
17
|
+
r = r.autenticate(username, password) if username.present?
|
18
|
+
r
|
19
|
+
end
|
20
|
+
|
21
|
+
def request_json(service_url_suffix, headers = {}, &body_data_proc)
|
22
|
+
request(service_url_suffix, headers.merge('Accept' => 'application/json')) do |body_data|
|
23
|
+
r = ::JSON.parse(body_data)
|
24
|
+
r = body_data_proc.call(r) if body_data_proc
|
25
|
+
r
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Addressable::URI]
|
30
|
+
def build_service_url(suffix)
|
31
|
+
s = ::Addressable::URI.parse(suffix)
|
32
|
+
r = ::Addressable::URI.parse(root_url)
|
33
|
+
r.path += s.path
|
34
|
+
r.query_values = r.query_values(::Array).if_present([]) +
|
35
|
+
s.query_values(::Array).if_present([])
|
36
|
+
r
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
|
5
|
+
module EacRest
|
6
|
+
class Entity
|
7
|
+
enable_simple_cache
|
8
|
+
common_constructor :api, :data
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def from_array_data(api, array_data, *args)
|
12
|
+
array_data.map { |item_data| new(api, item_data, *args) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'curb'
|
4
|
+
require 'eac_rest/response'
|
5
|
+
require 'eac_ruby_utils/core_ext'
|
6
|
+
require 'ostruct'
|
7
|
+
|
8
|
+
module EacRest
|
9
|
+
class Request
|
10
|
+
COMMON_MODIFIERS = %w[auth body_data verb].freeze
|
11
|
+
HASH_MODIFIERS = %w[header].freeze
|
12
|
+
MODIFIERS = COMMON_MODIFIERS + HASH_MODIFIERS.map(&:pluralize)
|
13
|
+
|
14
|
+
enable_immutable
|
15
|
+
immutable_accessor(*COMMON_MODIFIERS, type: :common)
|
16
|
+
immutable_accessor(*HASH_MODIFIERS, type: :hash)
|
17
|
+
|
18
|
+
enable_listable
|
19
|
+
lists.add_symbol :verb, :get, :delete, :options, :post, :put
|
20
|
+
|
21
|
+
common_constructor :url, :body_data_proc, default: [nil]
|
22
|
+
|
23
|
+
def autenticate(username, password)
|
24
|
+
auth(::OpenStruct.new(username: username, password: password))
|
25
|
+
end
|
26
|
+
|
27
|
+
def immutable_constructor_args
|
28
|
+
[url, body_data_proc]
|
29
|
+
end
|
30
|
+
|
31
|
+
def response
|
32
|
+
::EacRest::Response.new(build_curl, body_data_proc)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def build_curl
|
38
|
+
r = ::Curl::Easy.new(url)
|
39
|
+
MODIFIERS.each { |suffix| send("build_curl_#{suffix}", r) }
|
40
|
+
r
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_curl_auth(curl)
|
44
|
+
auth.if_present do |a|
|
45
|
+
curl.http_auth_types = :basic
|
46
|
+
curl.username = a.username
|
47
|
+
curl.password = a.password
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def build_curl_body_data(curl)
|
52
|
+
www_form_body_data_encoded.if_present { |v| curl.post_body = v }
|
53
|
+
end
|
54
|
+
|
55
|
+
def build_curl_headers(curl)
|
56
|
+
curl.headers.merge!(headers)
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_curl_verb(curl)
|
60
|
+
curl.set(
|
61
|
+
:customrequest,
|
62
|
+
verb.if_present(VERB_GET) { |v| self.class.lists.verb.value_validate!(v) }.to_s.upcase
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
def www_form_body_data_encoded
|
67
|
+
body_data.if_present do |v|
|
68
|
+
v = v.map { |k, vv| [k, vv] } if v.is_a?(::Hash)
|
69
|
+
v = URI.encode_www_form(v) if v.is_a?(::Array)
|
70
|
+
v.to_s
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/hash/conversions'
|
4
|
+
require 'eac_ruby_utils/core_ext'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module EacRest
|
8
|
+
class Response < ::StandardError
|
9
|
+
common_constructor :curl, :body_data_proc
|
10
|
+
|
11
|
+
def body_data
|
12
|
+
r = performed_curl.headers['Accept'].if_present(body_str) do |v|
|
13
|
+
method_name = "body_data_from_#{v.parameterize.underscore}"
|
14
|
+
respond_to?(method_name) ? send(method_name) : body_str
|
15
|
+
end
|
16
|
+
r = body_data_proc.call(r) if body_data_proc.present?
|
17
|
+
r
|
18
|
+
end
|
19
|
+
|
20
|
+
def body_data_or_raise
|
21
|
+
raise_unless_200
|
22
|
+
|
23
|
+
body_data
|
24
|
+
end
|
25
|
+
|
26
|
+
delegate :body_str, :headers, to: :performed_curl
|
27
|
+
|
28
|
+
def body_str_or_raise
|
29
|
+
raise_unless_200
|
30
|
+
|
31
|
+
body_str
|
32
|
+
end
|
33
|
+
|
34
|
+
def raise_unless_200
|
35
|
+
return nil if status == 200
|
36
|
+
|
37
|
+
raise self
|
38
|
+
end
|
39
|
+
|
40
|
+
def status
|
41
|
+
performed_curl.status.to_i
|
42
|
+
end
|
43
|
+
|
44
|
+
delegate :url, to: :curl
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
"URL: #{url}\nStatus: #{status}\nBody:\n\n#{body_str}"
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def body_data_from_application_json
|
53
|
+
::JSON.parse(body_str)
|
54
|
+
end
|
55
|
+
|
56
|
+
def body_data_from_application_xml
|
57
|
+
Hash.from_xml(body_str)
|
58
|
+
end
|
59
|
+
|
60
|
+
def perform
|
61
|
+
@perform ||= begin
|
62
|
+
curl.perform || raise("CURL perform failed for #{url}")
|
63
|
+
true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def performed_curl
|
68
|
+
perform
|
69
|
+
curl
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -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,81 @@
|
|
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
|
+
# @return [EacRubyUtils::BitArray] +self+.
|
31
|
+
def push_array(other_bit_array)
|
32
|
+
values_array.push(*other_bit_array.to_a)
|
33
|
+
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
# @param value [EacRubyUtils::Bit]
|
38
|
+
# @return [EacRubyUtils::Bit]
|
39
|
+
def push(value)
|
40
|
+
values_array.push(::EacRubyUtils::Bit.assert(value))
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [EacRubyUtils::BitArray]
|
44
|
+
def reverse
|
45
|
+
self.class.new(values_array.reverse)
|
46
|
+
end
|
47
|
+
|
48
|
+
# @param big_endian [Boolean]
|
49
|
+
# @return [EacRubyUtils::ByteArray]
|
50
|
+
def to_byte_array(big_endian = false)
|
51
|
+
unless count.modulo(::EacRubyUtils::Byte::BIT_COUNT).zero?
|
52
|
+
raise 'Bits returned is not multile of 8'
|
53
|
+
end
|
54
|
+
|
55
|
+
byte_bits_enumerator.each_with_object(::EacRubyUtils::ByteArray.new) do |e, a|
|
56
|
+
a << ::EacRubyUtils::Byte.from_bit_array(e, big_endian)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# @return [Array<Integer>]
|
61
|
+
def to_int_array
|
62
|
+
values_array.map(&:to_i)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def byte_bits_enumerator
|
68
|
+
::Enumerator.new do |y|
|
69
|
+
offset = 0
|
70
|
+
while offset < values_array.count
|
71
|
+
y.yield(values_array.slice(offset, ::EacRubyUtils::Byte::BIT_COUNT))
|
72
|
+
offset += ::EacRubyUtils::Byte::BIT_COUNT
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def values_array
|
78
|
+
@values_array ||= []
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,105 @@
|
|
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
|
+
ASSEMBLY_HEXADECIMAL_PREFIX = '$'
|
10
|
+
BIT_COUNT = 8
|
11
|
+
BIT_INDEX_RANGE = (0..7).freeze
|
12
|
+
VALUE_RANGE = (0..255).freeze
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def assert(obj)
|
16
|
+
return obj if obj.is_a?(self)
|
17
|
+
|
18
|
+
new(obj.to_i)
|
19
|
+
end
|
20
|
+
|
21
|
+
def from_bit_array(bit_array, big_endian = false)
|
22
|
+
bit_array = ::EacRubyUtils::BitArray.assert(bit_array)
|
23
|
+
raise ::ArgumentError, "Wrong bit array size: #{bit_array.size}" if
|
24
|
+
bit_array.size != BIT_COUNT
|
25
|
+
|
26
|
+
bit_array = bit_array.reverse if big_endian
|
27
|
+
bit_array.each_with_index.inject(new(0)) do |a, e|
|
28
|
+
a.bit_set(e[1], e[0])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def valid_bit_index?(value)
|
33
|
+
value.is_a?(::Integer) && BIT_INDEX_RANGE.include?(value)
|
34
|
+
end
|
35
|
+
|
36
|
+
def validate_bit_index(value)
|
37
|
+
return value if valid_bit_index?(value)
|
38
|
+
|
39
|
+
raise(::ArgumentError, "Invalid bit index: #{value} (Range: #{BIT_INDEX_RANGE})")
|
40
|
+
end
|
41
|
+
|
42
|
+
def valid_value?(value)
|
43
|
+
value.is_a?(::Integer) && VALUE_RANGE.include?(value)
|
44
|
+
end
|
45
|
+
|
46
|
+
def validate_value(value)
|
47
|
+
return value if valid_value?(value)
|
48
|
+
|
49
|
+
raise(::ArgumentError, "Invalid byte value: #{value} (Range: #{VALUE_RANGE})")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
attr_reader :value
|
54
|
+
compare_by :value
|
55
|
+
|
56
|
+
def initialize(value)
|
57
|
+
self.value = value
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param bit_index [Integer]
|
61
|
+
# @return [EacRubyUtils::Bit]
|
62
|
+
def [](bit_index)
|
63
|
+
bit_get(bit_index)
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param bit_index [Integer]
|
67
|
+
# @return [EacRubyUtils::Bit]
|
68
|
+
def bit_get(bit_index)
|
69
|
+
self.class.validate_bit_index(bit_index)
|
70
|
+
|
71
|
+
::EacRubyUtils::Bit.new((value & (1 << bit_index)) >> bit_index)
|
72
|
+
end
|
73
|
+
|
74
|
+
def bit_set(bit_index, bit_value)
|
75
|
+
self.class.validate_bit_index(bit_index)
|
76
|
+
bit = ::EacRubyUtils::Bit.assert(bit_value)
|
77
|
+
mask = (1 << bit_index)
|
78
|
+
self.class.new(bit.zero? ? value & ~mask : value | mask)
|
79
|
+
end
|
80
|
+
|
81
|
+
# @return [EacRubyUtils::BitArray]
|
82
|
+
def to_bit_array(range = BIT_INDEX_RANGE)
|
83
|
+
::EacRubyUtils::BitArray.new(range.map { |bit_index| self[bit_index] })
|
84
|
+
end
|
85
|
+
|
86
|
+
# @return [Integer]
|
87
|
+
def to_i
|
88
|
+
value
|
89
|
+
end
|
90
|
+
|
91
|
+
# @return [String]
|
92
|
+
def to_asm_hex
|
93
|
+
ASSEMBLY_HEXADECIMAL_PREFIX + to_hex
|
94
|
+
end
|
95
|
+
|
96
|
+
# @return [String]
|
97
|
+
def to_hex
|
98
|
+
value.to_s(16).upcase.rjust(2, '0')
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
attr_writer :value
|
104
|
+
end
|
105
|
+
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, :map, 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
|