eac_ruby_utils 0.38.0 → 0.43.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: 5fc852bc0ba01d1706a2f6c6dc7a0a09b1725b3dc3e54741abbf4c1d2022cac0
4
- data.tar.gz: 288360947e10bbef4f1b4cf100306e8bfcb7b5e146adead41184c96e4c66c2d3
3
+ metadata.gz: 17c2233b153a30506d6d62a3e8d89e35b9233d9ddb7043a4641a52ee214c2382
4
+ data.tar.gz: 7369e5d33ca13dd1827371d8fb257bf3ddab56bcd0a12b997eb6835349a94d61
5
5
  SHA512:
6
- metadata.gz: 579594be75a7160855c0b530cd4b5d9bf8f918f065b5e2d4938274d2395dd47f14ee7b827c8461fa6775a7962936ae729d90320124398d3f8220eb0c914243eb
7
- data.tar.gz: d287cd78c95da6e3d28c628c6059af23b3710d70f150672cbe13c47e80caa2989b3c93b084292e253380ef342128f780cc9b302cc09dfd36d9a0142ebf9a564b
6
+ metadata.gz: afc7ada9f7bd0f0c260d058db8d755d6778fa8d76e4dd549e8a0b7d6b6aac755ff18229da447bb2d8a6cac831b8d4a9c7585755fe16ed03ef6214653ee7b3f85
7
+ data.tar.gz: 4f227e60bcf60d4598a15cb4b8748696a602d3490d282e0ae2b4ca5546eb8161e4b011f22ab8647b39409a6bf5fb8055f78f4b6373c41e57c6351821274bfe1d
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class Boolean
5
+ class << self
6
+ def parse(value)
7
+ return parse_string(value) if value.is_a?(::String)
8
+ return parse_string(value.to_s) if value.is_a?(::Symbol)
9
+ return parse_number(value) if value.is_a?(::Number)
10
+
11
+ value ? true : false
12
+ end
13
+
14
+ private
15
+
16
+ def parse_string(value)
17
+ ['', 'n', 'no', 'f', 'false'].include?(value.strip.downcase) ? false : true
18
+ end
19
+
20
+ def parse_number(value)
21
+ value.zero?
22
+ end
23
+ end
24
+
25
+ attr_reader :value
26
+
27
+ def initialize(value)
28
+ @value = self.class.parse(value)
29
+ end
30
+ end
31
+ end
@@ -3,7 +3,7 @@
3
3
  module EacRubyUtils
4
4
  module Console
5
5
  class DocoptRunner
6
- DOCOPT_ERROR_EXIT_CODE = 0x22220000
6
+ DOCOPT_ERROR_EXIT_CODE = 0xC0
7
7
 
8
8
  class << self
9
9
  def run(options = {})
@@ -2,12 +2,16 @@
2
2
 
3
3
  require 'addressable'
4
4
  require 'eac_ruby_utils/envs/base_env'
5
+ require 'eac_ruby_utils/patches/object/if_present'
6
+ require 'eac_ruby_utils/patches/module/require_sub'
5
7
  require 'net/ssh'
6
8
  require 'shellwords'
7
9
 
8
10
  module EacRubyUtils
9
11
  module Envs
10
12
  class SshEnv < ::EacRubyUtils::Envs::BaseEnv
13
+ require_sub __FILE__, include_modules: true
14
+
11
15
  USER_PATTERN = /[a-z_][a-z0-9_-]*/.freeze
12
16
  HOSTNAME_PATTERN = /[^@]+/.freeze
13
17
  USER_HOSTNAME_PATTERN = /\A(?:(#{USER_PATTERN})@)?(#{HOSTNAME_PATTERN})\z/.freeze
@@ -49,17 +53,16 @@ module EacRubyUtils
49
53
  private
50
54
 
51
55
  def ssh_command_line
52
- r = %w[ssh]
53
- r += ['-p', uri.port] if uri.port.present?
54
- r += ssh_command_line_options
55
- r << user_hostname_uri
56
- r.map { |a| Shellwords.escape(a) }.join(' ')
56
+ (%w[ssh] +
57
+ %w[nodasho dasho port].flat_map { |m| send("ssh_command_line_#{m}_args") } +
58
+ [user_hostname_uri])
59
+ .map { |a| Shellwords.escape(a) }.join(' ')
57
60
  end
58
61
 
59
- def ssh_command_line_options
60
- r = []
61
- uri.query_values&.each { |k, v| r += ['-o', "#{k}=#{v}"] }
62
- r
62
+ def ssh_command_line_port_args
63
+ uri.port.if_present([]) do |v|
64
+ ['-p', v]
65
+ end
63
66
  end
64
67
 
65
68
  def user_hostname_uri
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/module/common_concern'
4
+ require 'eac_ruby_utils/patches/module/simple_cache'
5
+
6
+ module EacRubyUtils
7
+ module Envs
8
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
9
+ module DashoOptions
10
+ common_concern
11
+
12
+ module ClassMethods
13
+ def add_nodasho_option(name)
14
+ return if nodasho_options.include?(name)
15
+
16
+ nodasho_options << name
17
+ const_set("#{name.underscore}_option".upcase, name)
18
+ end
19
+
20
+ def nodasho_options
21
+ @nodasho_options ||= []
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def ssh_command_line_dasho_args
27
+ r = []
28
+ uri.query_values&.each do |k, v|
29
+ r += ['-o', "#{k}=#{v}"] unless nodasho_options.include?(k)
30
+ end
31
+ r
32
+ end
33
+
34
+ def ssh_command_line_nodasho_args
35
+ nodasho_options.flat_map do |option_name|
36
+ uri_query_value(option_name).if_present([]) do |option_value|
37
+ send("ssh_command_line_#{option_name.underscore}_args", option_value)
38
+ end
39
+ end
40
+ end
41
+
42
+ def nodasho_options
43
+ self.class.nodasho_options
44
+ end
45
+
46
+ def uri_query_value(name)
47
+ uri.query_values.if_present { |v| v[name] }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'eac_ruby_utils/envs/ssh_env/dasho_options'
5
+ require 'eac_ruby_utils/listable'
6
+ require 'eac_ruby_utils/patches/object/if_present'
7
+
8
+ module EacRubyUtils
9
+ module Envs
10
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
11
+ module IdentityFile
12
+ extend ::ActiveSupport::Concern
13
+
14
+ included do
15
+ include ::EacRubyUtils::Envs::SshEnv::DashoOptions
16
+ add_nodasho_option('IdentityFile')
17
+ end
18
+
19
+ def ssh_command_line_identity_file_args(value)
20
+ ['-i', value]
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'eac_ruby_utils/boolean'
5
+ require 'eac_ruby_utils/envs/ssh_env/dasho_options'
6
+
7
+ module EacRubyUtils
8
+ module Envs
9
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
10
+ module Quiet
11
+ extend ::ActiveSupport::Concern
12
+
13
+ included do
14
+ include ::EacRubyUtils::Envs::SshEnv::DashoOptions
15
+ add_nodasho_option('Quiet')
16
+ end
17
+
18
+ def ssh_command_line_quiet_args(value)
19
+ ::EacRubyUtils::Boolean.parse(value) ? ['-q'] : []
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'eac_ruby_utils/envs/ssh_env/dasho_options'
5
+ require 'eac_ruby_utils/listable'
6
+ require 'eac_ruby_utils/patches/object/if_present'
7
+
8
+ module EacRubyUtils
9
+ module Envs
10
+ class SshEnv < ::EacRubyUtils::Envs::BaseEnv
11
+ module Terminal
12
+ extend ::ActiveSupport::Concern
13
+
14
+ included do
15
+ include ::EacRubyUtils::Envs::SshEnv::DashoOptions
16
+ add_nodasho_option('Terminal')
17
+ include ::EacRubyUtils::Listable
18
+ lists.add_string :terminal_option, :auto, :disable, :enable, :force
19
+ end
20
+
21
+ def ssh_command_line_terminal_args(value)
22
+ self.class.lists.terminal_option.value_validate!(value)
23
+ case value
24
+ when TERMINAL_OPTION_AUTO then ENV['TERM'].present? ? %w[-t] : []
25
+ when TERMINAL_OPTION_DISABLE then ['-T']
26
+ when TERMINAL_OPTION_ENABLE then ['-t']
27
+ when TERMINAL_OPTION_FORCE then ['-tt']
28
+ else raise "Invalid conditional branch: #{value}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/filesystem_cache'
4
+ require 'tmpdir'
4
5
 
5
6
  module EacRubyUtils
6
7
  class << self
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/module/common_concern'
4
+ require 'eac_ruby_utils/require_sub'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ ::EacRubyUtils.require_sub __FILE__
9
+
10
+ common_concern do
11
+ include ::EacRubyUtils::Listable
12
+ lists.add_symbol :type, :array, :boolean, :common, :hash
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/inflector'
4
+ require 'eac_ruby_utils/immutable/base_accessor'
5
+ require 'eac_ruby_utils/patches/class/common_constructor'
6
+
7
+ module EacRubyUtils
8
+ module Immutable
9
+ class ArrayAccessor < ::EacRubyUtils::Immutable::BaseAccessor
10
+ def apply(klass)
11
+ accessor = self
12
+ klass.send(:define_method, name) do |value|
13
+ accessor.immutable_value_set(self, value)
14
+ end
15
+
16
+ klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do
17
+ accessor.immutable_value_get(self)
18
+ end
19
+ end
20
+
21
+ def immutable_value_get(object)
22
+ super || []
23
+ end
24
+
25
+ def immutable_value_set(object, value)
26
+ duplicate_object(object) do |old_value|
27
+ (old_value || []) + [value]
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ class BaseAccessor
6
+ common_constructor :name do
7
+ self.name = name.to_sym
8
+ end
9
+
10
+ def duplicate_object(object)
11
+ accessor_new_value = yield(immutable_value_get(object))
12
+ new_values = object.send(:immutable_values_get).merge(name => accessor_new_value)
13
+ r = object.class.new(*object.immutable_constructor_args)
14
+ r.send(:immutable_values_set, new_values)
15
+ r
16
+ end
17
+
18
+ def immutable_value_get(object)
19
+ object.send(:immutable_values_get)[name]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/common_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class BooleanAccessor < ::EacRubyUtils::Immutable::CommonAccessor
9
+ def apply(klass)
10
+ super
11
+ accessor = self
12
+ klass.send(:define_method, "#{name}?") { send(accessor.name) }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ module ClassMethods
6
+ def immutable_accessor(*accessors)
7
+ options = accessors.extract_options!
8
+ options[:type] ||= const_get('TYPE_COMMON')
9
+ accessors.each do |name|
10
+ class_name = options.fetch(:type).to_s.camelize + 'Accessor'
11
+ ::EacRubyUtils::Immutable.const_get(class_name).new(name).apply(self)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def imutable_single_accessor(name, options); end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/base_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class CommonAccessor < ::EacRubyUtils::Immutable::BaseAccessor
9
+ def apply(klass)
10
+ accessor = self
11
+ klass.send(:define_method, name) do |*args|
12
+ case args.count
13
+ when 0 then next accessor.immutable_value_get(self)
14
+ when 1 then next accessor.immutable_value_set(self, args.first)
15
+ else
16
+ raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 0..1)"
17
+ end
18
+ end
19
+ end
20
+
21
+ def immutable_value_get(object)
22
+ object.send(:immutable_values_get)[name]
23
+ end
24
+
25
+ def immutable_value_set(object, value)
26
+ duplicate_object(object) { |_old_value| value }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/immutable/base_accessor'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+
6
+ module EacRubyUtils
7
+ module Immutable
8
+ class HashAccessor < ::EacRubyUtils::Immutable::BaseAccessor
9
+ def apply(klass)
10
+ apply_get(klass)
11
+ apply_set(klass)
12
+ end
13
+
14
+ def immutable_value_get(object)
15
+ super || {}
16
+ end
17
+
18
+ def immutable_value_set(object, key, value)
19
+ duplicate_object(object) do |old_value|
20
+ (old_value || {}).merge(key => value)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def apply_get(klass)
27
+ accessor = self
28
+ klass.send(:define_method, ::ActiveSupport::Inflector.pluralize(name)) do
29
+ accessor.immutable_value_get(self)
30
+ end
31
+ end
32
+
33
+ def apply_set(klass)
34
+ accessor = self
35
+ klass.send(:define_method, name) do |*args|
36
+ case args.count
37
+ when 1 then next accessor.immutable_value_get(self, args[0])
38
+ when 2 then next accessor.immutable_value_set(self, *args[0..1])
39
+ else
40
+ raise ::ArgumentError, "wrong number of arguments (given #{args.count}, expected 1..2)"
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Immutable
5
+ module InstanceMethods
6
+ def immutable_constructor_args
7
+ []
8
+ end
9
+
10
+ private
11
+
12
+ def immutable_values_get
13
+ @immutable_values || {}
14
+ end
15
+
16
+ def immutable_values_set(new_values)
17
+ @immutable_values = new_values
18
+ end
19
+ end
20
+ end
21
+ end
@@ -33,6 +33,11 @@ module EacRubyUtils
33
33
  find_list_by_method(name) || super
34
34
  end
35
35
 
36
+ def hash_keys_validate!(hash, error_class = ::StandardError)
37
+ hash.keys.each { |key| value_validate!(key, error_class) }
38
+ hash
39
+ end
40
+
36
41
  def i18n_key
37
42
  "eac_ruby_utils.listable.#{class_i18n_key}.#{item}"
38
43
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patch'
4
+ require 'eac_ruby_utils/immutable'
5
+
6
+ class Module
7
+ def enable_immutable
8
+ ::EacRubyUtils.patch(self, ::EacRubyUtils::Immutable)
9
+ end
10
+ end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/inflector'
4
+ require 'eac_ruby_utils/listable'
5
+
3
6
  module EacRubyUtils
4
7
  class << self
5
8
  def require_sub(file, options = {})
@@ -8,15 +11,14 @@ module EacRubyUtils
8
11
  end
9
12
 
10
13
  class RequireSub
11
- BASE_OPTION_KEY = :base
12
- INCLUDE_MODULES_OPTION_KEY = :include_modules
13
- REQUIRE_DEPENDENCY_OPTION_KEY = :require_dependency
14
+ include ::EacRubyUtils::Listable
15
+ lists.add_symbol :option, :base, :include_modules, :require_dependency
14
16
 
15
17
  attr_reader :file, :options
16
18
 
17
19
  def initialize(file, options = {})
18
20
  @file = file
19
- @options = options
21
+ @options = self.class.lists.option.hash_keys_validate!(options)
20
22
  end
21
23
 
22
24
  def apply
@@ -26,8 +28,25 @@ module EacRubyUtils
26
28
 
27
29
  private
28
30
 
31
+ def active_support_require(path)
32
+ return false unless options[OPTION_REQUIRE_DEPENDENCY]
33
+
34
+ ::Kernel.require_dependency(path)
35
+ true
36
+ end
37
+
38
+ def autoload_require(path)
39
+ return false unless base?
40
+
41
+ basename = ::File.basename(path, '.*')
42
+ return false if basename.start_with?('_')
43
+
44
+ base.autoload ::ActiveSupport::Inflector.camelize(basename), path
45
+ true
46
+ end
47
+
29
48
  def include_modules
30
- return unless options[INCLUDE_MODULES_OPTION_KEY]
49
+ return unless options[OPTION_INCLUDE_MODULES]
31
50
 
32
51
  base.constants.each do |constant_name|
33
52
  constant = base.const_get(constant_name)
@@ -38,17 +57,25 @@ module EacRubyUtils
38
57
  end
39
58
 
40
59
  def base
41
- options[BASE_OPTION_KEY] || raise('Option :base not setted')
60
+ options[OPTION_BASE] || raise('Option :base not setted')
61
+ end
62
+
63
+ def base?
64
+ options[OPTION_BASE] ? true : false
65
+ end
66
+
67
+ def kernel_require(path)
68
+ ::Kernel.require(path)
42
69
  end
43
70
 
44
71
  def require_sub_files
45
72
  Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].sort.each do |path|
46
- if options[REQUIRE_DEPENDENCY_OPTION_KEY]
47
- require_dependency path
48
- else
49
- require path
50
- end
73
+ require_sub_file(path)
51
74
  end
52
75
  end
76
+
77
+ def require_sub_file(path)
78
+ active_support_require(path) || autoload_require(path) || kernel_require(path)
79
+ end
53
80
  end
54
81
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.38.0'
4
+ VERSION = '0.43.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.38.0
4
+ version: 0.43.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: 2020-07-02 00:00:00.000000000 Z
11
+ date: 2020-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -109,6 +109,7 @@ files:
109
109
  - README.rdoc
110
110
  - lib/eac_ruby_utils.rb
111
111
  - lib/eac_ruby_utils/arguments_consumer.rb
112
+ - lib/eac_ruby_utils/boolean.rb
112
113
  - lib/eac_ruby_utils/by_reference.rb
113
114
  - lib/eac_ruby_utils/common_concern.rb
114
115
  - lib/eac_ruby_utils/common_constructor.rb
@@ -139,6 +140,10 @@ files:
139
140
  - lib/eac_ruby_utils/envs/process.rb
140
141
  - lib/eac_ruby_utils/envs/spawn.rb
141
142
  - lib/eac_ruby_utils/envs/ssh_env.rb
143
+ - lib/eac_ruby_utils/envs/ssh_env/dasho_options.rb
144
+ - lib/eac_ruby_utils/envs/ssh_env/identity_file.rb
145
+ - lib/eac_ruby_utils/envs/ssh_env/quiet.rb
146
+ - lib/eac_ruby_utils/envs/ssh_env/terminal.rb
142
147
  - lib/eac_ruby_utils/filesystem_cache.rb
143
148
  - lib/eac_ruby_utils/fs.rb
144
149
  - lib/eac_ruby_utils/fs/extname.rb
@@ -148,6 +153,14 @@ files:
148
153
  - lib/eac_ruby_utils/fs/traversable.rb
149
154
  - lib/eac_ruby_utils/fs/traverser.rb
150
155
  - lib/eac_ruby_utils/fs_cache.rb
156
+ - lib/eac_ruby_utils/immutable.rb
157
+ - lib/eac_ruby_utils/immutable/array_accessor.rb
158
+ - lib/eac_ruby_utils/immutable/base_accessor.rb
159
+ - lib/eac_ruby_utils/immutable/boolean_accessor.rb
160
+ - lib/eac_ruby_utils/immutable/class_methods.rb
161
+ - lib/eac_ruby_utils/immutable/common_accessor.rb
162
+ - lib/eac_ruby_utils/immutable/hash_accessor.rb
163
+ - lib/eac_ruby_utils/immutable/instance_methods.rb
151
164
  - lib/eac_ruby_utils/listable.rb
152
165
  - lib/eac_ruby_utils/listable/class_methods.rb
153
166
  - lib/eac_ruby_utils/listable/instance_methods.rb
@@ -171,6 +184,7 @@ files:
171
184
  - lib/eac_ruby_utils/patches/module.rb
172
185
  - lib/eac_ruby_utils/patches/module/common_concern.rb
173
186
  - lib/eac_ruby_utils/patches/module/console_speaker.rb
187
+ - lib/eac_ruby_utils/patches/module/immutable.rb
174
188
  - lib/eac_ruby_utils/patches/module/listable.rb
175
189
  - lib/eac_ruby_utils/patches/module/patch.rb
176
190
  - lib/eac_ruby_utils/patches/module/require_sub.rb
@@ -228,8 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
242
  - !ruby/object:Gem::Version
229
243
  version: '0'
230
244
  requirements: []
231
- rubyforge_project:
232
- rubygems_version: 2.7.7
245
+ rubygems_version: 3.0.6
233
246
  signing_key:
234
247
  specification_version: 4
235
248
  summary: Utilities for E.A.C.'s Ruby projects.