eac_ruby_utils 0.79.0 → 0.82.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/eac_ruby_utils/abstract_methods.rb +2 -1
- data/lib/eac_ruby_utils/compare_by.rb +28 -0
- data/lib/eac_ruby_utils/enum.rb +102 -0
- data/lib/eac_ruby_utils/patches/module/compare_by.rb +9 -0
- data/lib/eac_ruby_utils/patches/object/debug.rb +1 -1
- data/lib/eac_ruby_utils/patches/pathname/mkpath_s.rb +12 -0
- data/lib/eac_ruby_utils/simple_cache.rb +16 -6
- data/lib/eac_ruby_utils/version.rb +1 -1
- metadata +6 -3
- data/lib/eac_ruby_utils/fs/logs.rb +0 -63
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7110d4a57ba58d345b748a2dd80906914f605c3c9b9df18e70580dc1f38d6d3a
|
4
|
+
data.tar.gz: 8cdaec95163b36b538b935765528cd738055950316aadccedd658a21b4f4cfb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8f74a4c7145d92d26870cfb1cdca4278ff3409753184150cf2fed90e6ec083c5e0e0214653c1d28fb4fbed9b65f2f1d29a1341b380117ed6050740540b0c699
|
7
|
+
data.tar.gz: dbfb7962f122f4b69e5bcecfeac9f4119e79f5c3393d76a06cae13cb5723bcf55f7122a50c14b6b5155edbf6245a275bceb0a5165670ad0a77c8288b434573e0
|
@@ -60,7 +60,8 @@ module EacRubyUtils
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def raise_abstract_method(method_name)
|
63
|
-
raise ::NoMethodError, "Abstract method \"#{method_name}\" hit in \"#{self}\""
|
63
|
+
raise ::NoMethodError, "Abstract method \"#{method_name}\" hit in \"#{self}\"" \
|
64
|
+
" (Class: #{self.class})"
|
64
65
|
end
|
65
66
|
end
|
66
67
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/common_concern/module_setup'
|
4
|
+
|
5
|
+
module EacRubyUtils
|
6
|
+
class CompareBy
|
7
|
+
attr_reader :fields
|
8
|
+
|
9
|
+
def initialize(fields)
|
10
|
+
@fields = fields
|
11
|
+
end
|
12
|
+
|
13
|
+
def apply(klass)
|
14
|
+
pself = self
|
15
|
+
klass.include(::Comparable)
|
16
|
+
%i[<=> eql?].each do |cmp_method|
|
17
|
+
klass.define_method(cmp_method) do |other|
|
18
|
+
pself.object_values(self).send(cmp_method, pself.object_values(other))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
klass.define_method(:hash) { pself.object_values(self).hash }
|
22
|
+
end
|
23
|
+
|
24
|
+
def object_values(object)
|
25
|
+
fields.map { |field| object.send(field) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/inflector'
|
4
|
+
|
5
|
+
module EacRubyUtils
|
6
|
+
# Similar to Java's enum type (https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html).
|
7
|
+
class Enum
|
8
|
+
class << self
|
9
|
+
def enum(key, *args, &block)
|
10
|
+
value = Value.new(self, key, args, &block)
|
11
|
+
raise ::ArgumentError, "#{self} already has a value with key=#{value.key}" if
|
12
|
+
value_set.include?(value)
|
13
|
+
|
14
|
+
value_set << value.apply
|
15
|
+
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def values
|
20
|
+
value_set.map(&:value)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def value_set
|
26
|
+
@value_set ||= []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :key
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def initialize(key)
|
35
|
+
@key = key
|
36
|
+
end
|
37
|
+
|
38
|
+
class Value
|
39
|
+
include ::Comparable
|
40
|
+
|
41
|
+
attr_reader :klass, :key, :args, :block
|
42
|
+
|
43
|
+
def initialize(klass, key, args, &block)
|
44
|
+
@klass = klass
|
45
|
+
@key = ::EacRubyUtils::Inflector.variableize(key.to_s).to_sym
|
46
|
+
@args = args
|
47
|
+
@block = block
|
48
|
+
end
|
49
|
+
|
50
|
+
def <=>(other)
|
51
|
+
key <=> other.key
|
52
|
+
end
|
53
|
+
|
54
|
+
def apply
|
55
|
+
define_constant
|
56
|
+
define_class_method
|
57
|
+
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [String]
|
62
|
+
def class_method_name
|
63
|
+
key.to_s
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [String]
|
67
|
+
def constant_name
|
68
|
+
class_method_name.upcase
|
69
|
+
end
|
70
|
+
|
71
|
+
def value
|
72
|
+
@value ||= uncached_value
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def define_class_method
|
78
|
+
the_value = self
|
79
|
+
klass.define_singleton_method(class_method_name) { the_value.value }
|
80
|
+
end
|
81
|
+
|
82
|
+
def define_constant
|
83
|
+
klass.const_set(constant_name, value)
|
84
|
+
end
|
85
|
+
|
86
|
+
def uncached_value
|
87
|
+
klass.new(key, *value_args)
|
88
|
+
end
|
89
|
+
|
90
|
+
def value_args
|
91
|
+
r = if block
|
92
|
+
block.call
|
93
|
+
elsif args.count == 1 && args.first.is_a?(::Enumerable)
|
94
|
+
args.first
|
95
|
+
else
|
96
|
+
args
|
97
|
+
end
|
98
|
+
r.is_a?(::Enumerable) ? r : [r]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -20,7 +20,7 @@ module EacRubyUtils
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def method_missing(method, *args, &block)
|
23
|
-
if respond_to?(
|
23
|
+
if respond_to?(uncached_method_name(method), true)
|
24
24
|
call_method_with_cache(method, args, &block)
|
25
25
|
else
|
26
26
|
super
|
@@ -37,27 +37,37 @@ module EacRubyUtils
|
|
37
37
|
|
38
38
|
def reset_cache(*keys)
|
39
39
|
if keys.any?
|
40
|
-
keys.each { |key| cache_keys.delete(key) }
|
40
|
+
keys.each { |key| cache_keys.delete(sanitize_cache_key(key)) }
|
41
41
|
else
|
42
42
|
@cache_keys = nil
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
def sanitize_cache_key(key)
|
47
|
+
key.to_s.to_sym
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def uncached_method_name(original_method_name)
|
53
|
+
::EacRubyUtils::SimpleCache.uncached_method_name(original_method_name)
|
54
|
+
end
|
55
|
+
|
46
56
|
private
|
47
57
|
|
48
58
|
def call_method_with_cache(method, args, &block)
|
49
59
|
raise 'Não é possível realizar o cache de métodos com bloco' if block
|
50
60
|
|
51
61
|
key = ([method] + args).join('@@@')
|
52
|
-
unless cache_keys.key?(key)
|
62
|
+
unless cache_keys.key?(sanitize_cache_key(key))
|
53
63
|
uncached_value = call_uncached_method(method, args)
|
54
|
-
cache_keys[key] = uncached_value
|
64
|
+
cache_keys[sanitize_cache_key(key)] = uncached_value
|
55
65
|
end
|
56
|
-
cache_keys[key]
|
66
|
+
cache_keys[sanitize_cache_key(key)]
|
57
67
|
end
|
58
68
|
|
59
69
|
def call_uncached_method(method, args)
|
60
|
-
send(
|
70
|
+
send(uncached_method_name(method), *args)
|
61
71
|
end
|
62
72
|
|
63
73
|
def cache_keys
|
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.82.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-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -107,10 +107,12 @@ files:
|
|
107
107
|
- lib/eac_ruby_utils/common_constructor/instance_initialize.rb
|
108
108
|
- lib/eac_ruby_utils/common_constructor/super_args.rb
|
109
109
|
- lib/eac_ruby_utils/compact.rb
|
110
|
+
- lib/eac_ruby_utils/compare_by.rb
|
110
111
|
- lib/eac_ruby_utils/context.rb
|
111
112
|
- lib/eac_ruby_utils/contextualizable.rb
|
112
113
|
- lib/eac_ruby_utils/core_ext.rb
|
113
114
|
- lib/eac_ruby_utils/custom_format.rb
|
115
|
+
- lib/eac_ruby_utils/enum.rb
|
114
116
|
- lib/eac_ruby_utils/envs.rb
|
115
117
|
- lib/eac_ruby_utils/envs/base_env.rb
|
116
118
|
- lib/eac_ruby_utils/envs/command.rb
|
@@ -130,7 +132,6 @@ files:
|
|
130
132
|
- lib/eac_ruby_utils/fs.rb
|
131
133
|
- lib/eac_ruby_utils/fs/clearable_directory.rb
|
132
134
|
- lib/eac_ruby_utils/fs/extname.rb
|
133
|
-
- lib/eac_ruby_utils/fs/logs.rb
|
134
135
|
- lib/eac_ruby_utils/fs/temp.rb
|
135
136
|
- lib/eac_ruby_utils/fs/temp/directory.rb
|
136
137
|
- lib/eac_ruby_utils/fs/temp/file.rb
|
@@ -176,6 +177,7 @@ files:
|
|
176
177
|
- lib/eac_ruby_utils/patches/module.rb
|
177
178
|
- lib/eac_ruby_utils/patches/module/abstract_methods.rb
|
178
179
|
- lib/eac_ruby_utils/patches/module/common_concern.rb
|
180
|
+
- lib/eac_ruby_utils/patches/module/compare_by.rb
|
179
181
|
- lib/eac_ruby_utils/patches/module/context.rb
|
180
182
|
- lib/eac_ruby_utils/patches/module/i18n_translate.rb
|
181
183
|
- lib/eac_ruby_utils/patches/module/immutable.rb
|
@@ -196,6 +198,7 @@ files:
|
|
196
198
|
- lib/eac_ruby_utils/patches/object/to_pathname.rb
|
197
199
|
- lib/eac_ruby_utils/patches/pathname.rb
|
198
200
|
- lib/eac_ruby_utils/patches/pathname/basename_sub.rb
|
201
|
+
- lib/eac_ruby_utils/patches/pathname/mkpath_s.rb
|
199
202
|
- lib/eac_ruby_utils/patches/pathname/parent_n.rb
|
200
203
|
- lib/eac_ruby_utils/patches/regexp.rb
|
201
204
|
- lib/eac_ruby_utils/patches/regexp/if_match.rb
|
@@ -1,63 +0,0 @@
|
|
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
|