eac_ruby_utils 0.59.0 → 0.63.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eac_ruby_utils/filesystem_cache.rb +6 -0
- data/lib/eac_ruby_utils/fs/logs.rb +63 -0
- data/lib/eac_ruby_utils/inflector.rb +9 -1
- data/lib/eac_ruby_utils/listable/list.rb +3 -2
- data/lib/eac_ruby_utils/patches/kernel/nyi.rb +4 -2
- data/lib/eac_ruby_utils/patches/regexp/to_parser.rb +10 -0
- data/lib/eac_ruby_utils/patches/string/inflector.rb +4 -2
- data/lib/eac_ruby_utils/regexp_parser.rb +34 -0
- data/lib/eac_ruby_utils/settings_provider.rb +10 -29
- data/lib/eac_ruby_utils/settings_provider/setting_value.rb +69 -0
- data/lib/eac_ruby_utils/simple_cache.rb +18 -5
- data/lib/eac_ruby_utils/version.rb +1 -1
- metadata +23 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 988305393f16d7782424d4d46c3cfa88d805d1a06b813e87027a9f44ea2b5ce3
|
4
|
+
data.tar.gz: c8c55c9c290a618bcf090a9c7d59bd5a51055d91284ca2e8c411fdb6898f3ee0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7c3cdfdef8b47a669ef14010c8a76399e4bd3febe7c860123a36a3b54dabce947ccbb5231031a11ea11540977930f10a6ca03079e8de86a47f7b4f048b4d0dc
|
7
|
+
data.tar.gz: b1ff9b89451e86ce8a76a05dff08b19b21a77c2c129f5390a2a9400b49bc5a9812eb44ec9591a323c5fb8f419d83da8321d4251cbd5ca0d07409277dabf8e82b
|
@@ -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
|
@@ -7,11 +7,19 @@ module EacRubyUtils
|
|
7
7
|
class << self
|
8
8
|
VARIABLE_NAME_PATTERN = /[_a-z][_a-z0-9]*/i.freeze
|
9
9
|
|
10
|
-
|
10
|
+
# Convert a string to a variable format: first character as a lowercase letter or underscore
|
11
|
+
# and other as a lowercase letter, underscore or numbers.
|
12
|
+
# @param string [String] The source string.
|
13
|
+
# @param validate [Boolean] Affect the outcome when the result builded is not in a valid
|
14
|
+
# variable format. If `true`, it raises a {ArgumentError}. If `false`, return `nil`.
|
15
|
+
# @return [String, nil]
|
16
|
+
# @raise [ArgumentError]
|
17
|
+
def variableize(string, validate = true)
|
11
18
|
r = ::ActiveSupport::Inflector.transliterate(string).gsub(/[^_a-z0-9]/i, '_')
|
12
19
|
.gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
|
13
20
|
m = VARIABLE_NAME_PATTERN.match(r)
|
14
21
|
return r if m
|
22
|
+
return nil unless validate
|
15
23
|
|
16
24
|
raise ::ArgumentError, "Invalid variable name \"#{r}\" was generated " \
|
17
25
|
"from string \"#{string}\""
|
@@ -54,8 +54,9 @@ module EacRubyUtils
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def value_validate!(value, error_class = ::StandardError)
|
57
|
-
value_valid?(value)
|
58
|
-
|
57
|
+
return value if value_valid?(value)
|
58
|
+
|
59
|
+
raise(error_class, "Invalid value: \"#{value}\" (Valid: #{values_to_s})")
|
59
60
|
end
|
60
61
|
|
61
62
|
def values_to_s
|
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
module Kernel
|
4
4
|
# Raise exception with text "Not yet implemented".
|
5
|
-
def nyi
|
6
|
-
|
5
|
+
def nyi(message = nil)
|
6
|
+
s = "Not yet implemented (Called in #{caller.first})"
|
7
|
+
s += ": #{message}" if message
|
8
|
+
raise s
|
7
9
|
end
|
8
10
|
end
|
@@ -3,7 +3,9 @@
|
|
3
3
|
require 'eac_ruby_utils/inflector'
|
4
4
|
|
5
5
|
class String
|
6
|
-
|
7
|
-
|
6
|
+
# Shortcut to `EacRubyUtils::Inflector.variableize(self, ...)`.
|
7
|
+
# @see EacRubyUtils::Inflector.variableize
|
8
|
+
def variableize(validate = true)
|
9
|
+
::EacRubyUtils::Inflector.variableize(self, validate)
|
8
10
|
end
|
9
11
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EacRubyUtils
|
4
|
+
class RegexpParser
|
5
|
+
attr_reader :pattern, :builder_proc
|
6
|
+
|
7
|
+
def initialize(pattern, &builder_proc)
|
8
|
+
@pattern = pattern
|
9
|
+
@builder_proc = builder_proc
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(string)
|
13
|
+
internal_parse(string)[1]
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse!(string)
|
17
|
+
match, result = internal_parse(string)
|
18
|
+
return result if match
|
19
|
+
|
20
|
+
raise ::ArgumentError, "String \"#{string}\" does not match pattern \"#{pattern}\""
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def internal_parse(string)
|
26
|
+
m = pattern.match(string)
|
27
|
+
if m
|
28
|
+
[true, builder_proc ? builder_proc.call(m) : m]
|
29
|
+
else
|
30
|
+
[false, nil]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,15 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
35
|
+
setting_value_instance(key).value_by_method
|
50
36
|
end
|
51
37
|
|
52
38
|
def setting_value_by_settings_object(key)
|
53
|
-
|
39
|
+
setting_value_instance(key).value_by_settings_object
|
54
40
|
end
|
55
41
|
|
56
|
-
|
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
|
@@ -2,12 +2,18 @@
|
|
2
2
|
|
3
3
|
module EacRubyUtils
|
4
4
|
module SimpleCache
|
5
|
-
|
5
|
+
UNCACHED_METHOD_NAME_SUFFIX = '_uncached'
|
6
|
+
UNCACHED_METHOD_PATTERN = /\A(\s+)_#{::Regexp.quote(UNCACHED_METHOD_NAME_SUFFIX)}\z/.freeze
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def uncached_method_name(method_name)
|
10
|
+
"#{method_name}#{UNCACHED_METHOD_NAME_SUFFIX}"
|
11
|
+
end
|
12
|
+
end
|
6
13
|
|
7
14
|
def method_missing(method, *args, &block)
|
8
|
-
|
9
|
-
|
10
|
-
call_method_with_cache(uncached_method, args, &block)
|
15
|
+
if respond_to?(::EacRubyUtils::SimpleCache.uncached_method_name(method), true)
|
16
|
+
call_method_with_cache(method, args, &block)
|
11
17
|
else
|
12
18
|
super
|
13
19
|
end
|
@@ -35,10 +41,17 @@ module EacRubyUtils
|
|
35
41
|
raise 'Não é possível realizar o cache de métodos com bloco' if block
|
36
42
|
|
37
43
|
key = ([method] + args).join('@@@')
|
38
|
-
|
44
|
+
unless cache_keys.key?(key)
|
45
|
+
uncached_value = call_uncached_method(method, args)
|
46
|
+
cache_keys[key] = uncached_value
|
47
|
+
end
|
39
48
|
cache_keys[key]
|
40
49
|
end
|
41
50
|
|
51
|
+
def call_uncached_method(method, args)
|
52
|
+
send(::EacRubyUtils::SimpleCache.uncached_method_name(method), *args)
|
53
|
+
end
|
54
|
+
|
42
55
|
def cache_keys
|
43
56
|
@cache_keys ||= {}
|
44
57
|
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.
|
4
|
+
version: 0.63.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-
|
11
|
+
date: 2021-05-06 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
|
@@ -86,20 +100,14 @@ dependencies:
|
|
86
100
|
requirements:
|
87
101
|
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0.
|
90
|
-
- - ">="
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
version: 0.1.1
|
103
|
+
version: '0.2'
|
93
104
|
type: :development
|
94
105
|
prerelease: false
|
95
106
|
version_requirements: !ruby/object:Gem::Requirement
|
96
107
|
requirements:
|
97
108
|
- - "~>"
|
98
109
|
- !ruby/object:Gem::Version
|
99
|
-
version: '0.
|
100
|
-
- - ">="
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
version: 0.1.1
|
110
|
+
version: '0.2'
|
103
111
|
description:
|
104
112
|
email:
|
105
113
|
executables: []
|
@@ -162,6 +170,7 @@ files:
|
|
162
170
|
- lib/eac_ruby_utils/fs.rb
|
163
171
|
- lib/eac_ruby_utils/fs/clearable_directory.rb
|
164
172
|
- lib/eac_ruby_utils/fs/extname.rb
|
173
|
+
- lib/eac_ruby_utils/fs/logs.rb
|
165
174
|
- lib/eac_ruby_utils/fs/temp.rb
|
166
175
|
- lib/eac_ruby_utils/fs/temp/directory.rb
|
167
176
|
- lib/eac_ruby_utils/fs/temp/file.rb
|
@@ -226,6 +235,7 @@ files:
|
|
226
235
|
- lib/eac_ruby_utils/patches/pathname/basename_sub.rb
|
227
236
|
- lib/eac_ruby_utils/patches/regexp.rb
|
228
237
|
- lib/eac_ruby_utils/patches/regexp/if_match.rb
|
238
|
+
- lib/eac_ruby_utils/patches/regexp/to_parser.rb
|
229
239
|
- lib/eac_ruby_utils/patches/string.rb
|
230
240
|
- lib/eac_ruby_utils/patches/string/inflector.rb
|
231
241
|
- lib/eac_ruby_utils/patches/time.rb
|
@@ -234,6 +244,7 @@ files:
|
|
234
244
|
- lib/eac_ruby_utils/paths_hash/entry_key_error.rb
|
235
245
|
- lib/eac_ruby_utils/paths_hash/node.rb
|
236
246
|
- lib/eac_ruby_utils/paths_hash/path_search.rb
|
247
|
+
- lib/eac_ruby_utils/regexp_parser.rb
|
237
248
|
- lib/eac_ruby_utils/require_sub.rb
|
238
249
|
- lib/eac_ruby_utils/rspec.rb
|
239
250
|
- lib/eac_ruby_utils/rspec/conditional.rb
|
@@ -242,6 +253,7 @@ files:
|
|
242
253
|
- lib/eac_ruby_utils/ruby/command.rb
|
243
254
|
- lib/eac_ruby_utils/ruby/on_clean_environment.rb
|
244
255
|
- lib/eac_ruby_utils/settings_provider.rb
|
256
|
+
- lib/eac_ruby_utils/settings_provider/setting_value.rb
|
245
257
|
- lib/eac_ruby_utils/simple_cache.rb
|
246
258
|
- lib/eac_ruby_utils/struct.rb
|
247
259
|
- lib/eac_ruby_utils/templates.rb
|
@@ -275,7 +287,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
275
287
|
- !ruby/object:Gem::Version
|
276
288
|
version: '0'
|
277
289
|
requirements: []
|
278
|
-
rubygems_version: 3.0.
|
290
|
+
rubygems_version: 3.0.9
|
279
291
|
signing_key:
|
280
292
|
specification_version: 4
|
281
293
|
summary: Utilities for E.A.C.'s Ruby projects.
|