eac_ruby_utils 0.57.2 → 0.61.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: e9a5ba0cb4987def93f005feb8c7d109afe4f5e202939d9a5db9e64eb8d08194
4
- data.tar.gz: 9c7632467e434195994afdf69ada8ea8820e5df639709e79cae7cb8543169129
3
+ metadata.gz: 9a49138197a3fe16cb9a5e1f22326be1240a10f568c3ddc913e9c6f2a7501475
4
+ data.tar.gz: e218f41a27514ca27c634619ae7eecc3fe4d7442ba546a9c24f584d437ecfe77
5
5
  SHA512:
6
- metadata.gz: 92cfc25fcb83ec39aff6136acd20fc30a5a9eacb493765ba034f6fa9291e022c931b55d80350fefe605a126a7f9c61b75e6e2b78e6d24b0b627912fde7a4c0fc
7
- data.tar.gz: bedc1cd2d75146ca942894d9105e4c6ce02283996c7b1192cd26f93024895feb3a3b02c5dcab0e2f50d0ef5c273c3cdd494d576f55078aa1d5c27712a1679a0a
6
+ metadata.gz: f3000972a617e1f15500506c4e61909c50418dd9794c6516154962dd5f55b1173b612aa9d1bf0c23dbaee205ccc01abd93b1349f80147c53bc4f3640bb7739e6
7
+ data.tar.gz: 9a021a5edb6bb22208d22cfc45fd17a486f9cd6c919a8a9495b2afe3b75a51b238172fd350f6977edd02669f7e77beab1ed7e53f7070bd36ebfa058dc52273a4
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/filters'
4
+ require 'eac_ruby_utils/fs/temp'
5
+ require 'filesize'
6
+
7
+ module EacRubyUtils
8
+ module Fs
9
+ class Logs
10
+ TRUNCATE_DEFAULT_LENGTH = 1000
11
+ TRUNCATE_APPEND_TEXT = '(...) '
12
+
13
+ def [](label)
14
+ log_set.fetch(sanitize_label(label))
15
+ end
16
+
17
+ def add(label)
18
+ log_set[sanitize_label(label)] = ::EacRubyUtils::Fs::Temp.file
19
+
20
+ self
21
+ end
22
+
23
+ def remove_all
24
+ log_set.each_key { |label| remove(label) }
25
+
26
+ self
27
+ end
28
+
29
+ def remove(label)
30
+ log_set.fetch(sanitize_label(label)).remove
31
+ log_set.delete(sanitize_label(label))
32
+ end
33
+
34
+ def truncate(label, length = TRUNCATE_DEFAULT_LENGTH)
35
+ content = self[label].read.strip
36
+ return content if content.length <= TRUNCATE_DEFAULT_LENGTH
37
+
38
+ TRUNCATE_APPEND_TEXT + content[content.length - length + TRUNCATE_APPEND_TEXT.length,
39
+ length - TRUNCATE_APPEND_TEXT.length]
40
+ end
41
+
42
+ def truncate_all(length = TRUNCATE_DEFAULT_LENGTH)
43
+ s = "Files: #{log_set.length}\n"
44
+ log_set.each do |label, path|
45
+ x = truncate(label, length)
46
+ y = [label, path, ::Filesize.from("#{path.size} B").pretty].join(' / ')
47
+ s += x.blank? ? ">>> #{y} (Blank) <<<" : ">>> #{y}\n#{x}\n<<< #{y}\n"
48
+ end
49
+ s
50
+ end
51
+
52
+ private
53
+
54
+ def sanitize_label(label)
55
+ label.to_sym
56
+ end
57
+
58
+ def log_set
59
+ @log_set ||= {}
60
+ end
61
+ end
62
+ end
63
+ end
@@ -10,26 +10,36 @@ module EacRubyUtils
10
10
  class << self
11
11
  TIMEDATECTL_TIMEZONE_LINE_PATTERN = %r{\s*Time zone:\s*(\S+/\S+)\s}.freeze
12
12
 
13
+ # @return [ActiveSupport::TimeZone]
13
14
  def auto
14
15
  %w[tz_env debian_config offset].lazy.map { |s| send("by_#{s}") }.find(&:present?)
15
16
  end
16
17
 
18
+ def auto_set
19
+ ::Time.zone = auto
20
+ end
21
+
22
+ # @return [ActiveSupport::TimeZone]
17
23
  def by_debian_config
18
24
  path = ::Pathname.new(DEBIAN_CONFIG_PATH)
19
- path.exist? ? path.read.strip.presence : nil
25
+ path.exist? ? path.read.strip.if_present { |v| ::ActiveSupport::TimeZone[v] } : nil
20
26
  end
21
27
 
28
+ # @return [ActiveSupport::TimeZone]
22
29
  def by_offset
23
- ::ActiveSupport::TimeZone[::Time.now.getlocal.gmt_offset].name
30
+ ::ActiveSupport::TimeZone[::Time.now.getlocal.gmt_offset]
24
31
  end
25
32
 
33
+ # @return [ActiveSupport::TimeZone]
26
34
  def by_timedatectl
27
35
  executable = ::EacRubyUtils::Envs.local.executable('timedatectl', '--version')
28
36
  return nil unless executable.exist?
29
37
 
30
38
  TIMEDATECTL_TIMEZONE_LINE_PATTERN.if_match(executable.command.execute!) { |m| m[1] }
39
+ .if_present { |v| ::ActiveSupport::TimeZone[v] }
31
40
  end
32
41
 
42
+ # @return [ActiveSupport::TimeZone]
33
43
  def by_tz_env
34
44
  ENV['TZ'].presence
35
45
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patch'
4
+ require 'eac_ruby_utils/settings_provider'
5
+
6
+ class Class
7
+ def enable_settings_provider
8
+ ::EacRubyUtils.patch(self, ::EacRubyUtils::SettingsProvider)
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub(__FILE__)
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kernel
4
+ # Raise exception with text "Not yet implemented".
5
+ def nyi
6
+ raise "Not yet implemented (Called in #{caller.first})"
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Object
4
+ # @return +block.call(self)+ if +self+ is not nil, +default_value+ otherwise.
5
+ def if_not_nil(default_value = nil)
6
+ return default_value if nil?
7
+
8
+ block_given? ? yield(self) : self
9
+ end
10
+
11
+ # @return +yield+ if +self+ is nil, +self+ otherwise.
12
+ def if_nil
13
+ return yield if nil? && block_given?
14
+
15
+ self
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/local_time_zone'
4
+
5
+ class Time
6
+ class << self
7
+ def required_zone
8
+ zone || ::EacRubyUtils::LocalTimeZone.auto || raise('No zone set or discovered')
9
+ end
10
+ end
11
+ end
@@ -1,15 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string/inflections'
4
- require 'eac_ruby_utils/options_consumer'
3
+ require 'eac_ruby_utils/require_sub'
5
4
 
6
5
  module EacRubyUtils
7
6
  # Provide a option by constant, method or options object.
8
7
  module SettingsProvider
8
+ ::EacRubyUtils.require_sub __FILE__, base: self
9
+
9
10
  def setting_constant_name(key, fullname = false)
10
- name = key.to_s.underscore.upcase
11
- name = "#{self.class.name}::#{name}" if fullname
12
- name
11
+ setting_value_instance(key).constant_name(fullname)
13
12
  end
14
13
 
15
14
  def setting_search_order
@@ -25,41 +24,23 @@ module EacRubyUtils
25
24
  end
26
25
 
27
26
  def setting_value(key, options = {})
28
- options = parse_setting_value_options(options)
29
- options.order.each do |method|
30
- value = send("setting_value_by_#{method}", key)
31
- return value if value
32
- end
33
- return nil unless options.required
34
-
35
- raise "Setting \"#{key}\" not found. Supply in #{settings_object_name}, implement a " \
36
- "\"#{key}\" method or declare a #{setting_constant_name(key, true)} constant."
27
+ setting_value_instance(key, options).value
37
28
  end
38
29
 
39
30
  def setting_value_by_constant(key)
40
- constant_name = setting_constant_name(key)
41
- begin
42
- self.class.const_get(constant_name)
43
- rescue NameError
44
- nil
45
- end
31
+ setting_value_instance(key).value_by_constant
46
32
  end
47
33
 
48
34
  def setting_value_by_method(key)
49
- respond_to?(key) ? send(key) : nil
35
+ setting_value_instance(key).value_by_method
50
36
  end
51
37
 
52
38
  def setting_value_by_settings_object(key)
53
- settings_object[key.to_s] || settings_object[key.to_sym]
39
+ setting_value_instance(key).value_by_settings_object
54
40
  end
55
41
 
56
- private
57
-
58
- def parse_setting_value_options(options)
59
- r = ::EacRubyUtils::OptionsConsumer.new(options).consume_all(:required, :order, ostruct: true)
60
- r.required = true if r.required.nil?
61
- r.order = setting_search_order if r.order.nil?
62
- r
42
+ def setting_value_instance(key, options = {})
43
+ ::EacRubyUtils::SettingsProvider::SettingValue.new(self, key, options)
63
44
  end
64
45
  end
65
46
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'eac_ruby_utils/listable'
5
+ require 'eac_ruby_utils/simple_cache'
6
+ require 'eac_ruby_utils/struct'
7
+
8
+ module EacRubyUtils
9
+ module SettingsProvider
10
+ class SettingValue
11
+ include ::EacRubyUtils::Listable
12
+ include ::EacRubyUtils::SimpleCache
13
+
14
+ attr_reader :source, :key, :options
15
+ lists.add_symbol :option, :default, :order, :required
16
+
17
+ def initialize(source, key, options)
18
+ @source = source
19
+ @key = key
20
+ @options = options
21
+ end
22
+
23
+ def constant_name(fullname = false)
24
+ name = key.to_s.underscore.upcase
25
+ name = "#{source.class.name}::#{name}" if fullname
26
+ name
27
+ end
28
+
29
+ def value
30
+ parsed_options.order.each do |method|
31
+ value = send("value_by_#{method}")
32
+ return value if value
33
+ end
34
+ return parsed_options.default if parsed_options.respond_to?(OPTION_DEFAULT)
35
+ return nil unless parsed_options.required
36
+
37
+ raise_key_not_found
38
+ end
39
+
40
+ def value_by_constant
41
+ source.class.const_get(constant_name)
42
+ rescue NameError
43
+ nil
44
+ end
45
+
46
+ def value_by_method
47
+ source.respond_to?(key, true) ? source.send(key) : nil
48
+ end
49
+
50
+ def value_by_settings_object
51
+ source.settings_object[key.to_s] || source.settings_object[key.to_sym]
52
+ end
53
+
54
+ private
55
+
56
+ def parsed_options_uncached
57
+ r = self.class.lists.option.hash_keys_validate!(options.symbolize_keys)
58
+ r[:required] = true unless r.key?(OPTION_REQUIRED)
59
+ r[:order] = source.setting_search_order if r[OPTION_ORDER].nil?
60
+ ::EacRubyUtils::Struct.new(r)
61
+ end
62
+
63
+ def raise_key_not_found
64
+ raise "Setting \"#{key}\" not found. Supply in #{source.settings_object_name}, implement "\
65
+ "a \"#{key}\" method or declare a #{constant_name(true)} constant."
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.57.2'
4
+ VERSION = '0.61.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.57.2
4
+ version: 0.61.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: 2021-01-16 00:00:00.000000000 Z
11
+ date: 2021-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.6.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: filesize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: net-ssh
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -162,6 +176,7 @@ files:
162
176
  - lib/eac_ruby_utils/fs.rb
163
177
  - lib/eac_ruby_utils/fs/clearable_directory.rb
164
178
  - lib/eac_ruby_utils/fs/extname.rb
179
+ - lib/eac_ruby_utils/fs/logs.rb
165
180
  - lib/eac_ruby_utils/fs/temp.rb
166
181
  - lib/eac_ruby_utils/fs/temp/directory.rb
167
182
  - lib/eac_ruby_utils/fs/temp/file.rb
@@ -193,6 +208,7 @@ files:
193
208
  - lib/eac_ruby_utils/patches.rb
194
209
  - lib/eac_ruby_utils/patches/class.rb
195
210
  - lib/eac_ruby_utils/patches/class/common_constructor.rb
211
+ - lib/eac_ruby_utils/patches/class/settings_provider.rb
196
212
  - lib/eac_ruby_utils/patches/enumerable.rb
197
213
  - lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
198
214
  - lib/eac_ruby_utils/patches/enumerator.rb
@@ -201,6 +217,8 @@ files:
201
217
  - lib/eac_ruby_utils/patches/hash.rb
202
218
  - lib/eac_ruby_utils/patches/hash/options_consumer.rb
203
219
  - lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb
220
+ - lib/eac_ruby_utils/patches/kernel.rb
221
+ - lib/eac_ruby_utils/patches/kernel/nyi.rb
204
222
  - lib/eac_ruby_utils/patches/module.rb
205
223
  - lib/eac_ruby_utils/patches/module/abstract_methods.rb
206
224
  - lib/eac_ruby_utils/patches/module/common_concern.rb
@@ -214,6 +232,7 @@ files:
214
232
  - lib/eac_ruby_utils/patches/object.rb
215
233
  - lib/eac_ruby_utils/patches/object/asserts.rb
216
234
  - lib/eac_ruby_utils/patches/object/debug.rb
235
+ - lib/eac_ruby_utils/patches/object/if_nil.rb
217
236
  - lib/eac_ruby_utils/patches/object/if_present.rb
218
237
  - lib/eac_ruby_utils/patches/object/if_respond.rb
219
238
  - lib/eac_ruby_utils/patches/object/template.rb
@@ -225,7 +244,7 @@ files:
225
244
  - lib/eac_ruby_utils/patches/string.rb
226
245
  - lib/eac_ruby_utils/patches/string/inflector.rb
227
246
  - lib/eac_ruby_utils/patches/time.rb
228
- - lib/eac_ruby_utils/patches/time/default_time_zone_set.rb
247
+ - lib/eac_ruby_utils/patches/time/required_zone.rb
229
248
  - lib/eac_ruby_utils/paths_hash.rb
230
249
  - lib/eac_ruby_utils/paths_hash/entry_key_error.rb
231
250
  - lib/eac_ruby_utils/paths_hash/node.rb
@@ -238,6 +257,7 @@ files:
238
257
  - lib/eac_ruby_utils/ruby/command.rb
239
258
  - lib/eac_ruby_utils/ruby/on_clean_environment.rb
240
259
  - lib/eac_ruby_utils/settings_provider.rb
260
+ - lib/eac_ruby_utils/settings_provider/setting_value.rb
241
261
  - lib/eac_ruby_utils/simple_cache.rb
242
262
  - lib/eac_ruby_utils/struct.rb
243
263
  - lib/eac_ruby_utils/templates.rb
@@ -271,7 +291,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
271
291
  - !ruby/object:Gem::Version
272
292
  version: '0'
273
293
  requirements: []
274
- rubygems_version: 3.0.8
294
+ rubygems_version: 3.0.9
275
295
  signing_key:
276
296
  specification_version: 4
277
297
  summary: Utilities for E.A.C.'s Ruby projects.
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/core_ext/time/zones'
4
- require 'eac_ruby_utils/local_time_zone'
5
-
6
- ::Time.zone = ::EacRubyUtils::LocalTimeZone.auto