eac_fs 0.5.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eac_fs/logs/file.rb +52 -0
- data/lib/eac_fs/logs.rb +59 -0
- data/lib/eac_fs/patches/object/fs_cache.rb +4 -2
- data/lib/eac_fs/traversable.rb +45 -0
- data/lib/eac_fs/traverser.rb +76 -0
- data/lib/eac_fs/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d10da0054341f15b13229c38ee23f562d031eed02e1551e1672d76353819e57
|
4
|
+
data.tar.gz: ef5ce5e89c868acd751620330a295bca3382e1502a75eb4e071e4f3ce9b2a57d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5f015ff3178af6b1b66912655f30694853c49ce7f18e92e977a7ec3c7befc90bc637522eea84a407f72563d8d766b40c1497f41ce76c5a23ebdeea21c27931a
|
7
|
+
data.tar.gz: 5d6744fae96e6ea41bc8e17559924a80dc7d8e2a1081db464145a9fedd16d535d16bf05d4a871d6981b249fe5c7437d05d5f555a7a47203651a0e4756b3e3d35
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
|
5
|
+
module EacFs
|
6
|
+
class Logs
|
7
|
+
class File
|
8
|
+
enable_simple_cache
|
9
|
+
common_constructor :label
|
10
|
+
|
11
|
+
TRUNCATE_DEFAULT_LENGTH = 1000
|
12
|
+
TRUNCATE_APPEND_TEXT = '(...) '
|
13
|
+
|
14
|
+
delegate :remove, to: :file
|
15
|
+
|
16
|
+
def file_size
|
17
|
+
file.file? ? file.size : 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def pretty_file_size
|
21
|
+
::Filesize.from("#{file_size} B").pretty
|
22
|
+
end
|
23
|
+
|
24
|
+
# @param length [Integer]
|
25
|
+
# @return [String]
|
26
|
+
def truncate(length = TRUNCATE_DEFAULT_LENGTH)
|
27
|
+
content = file.file? ? file.read.strip : ''
|
28
|
+
return content if content.length <= TRUNCATE_DEFAULT_LENGTH
|
29
|
+
|
30
|
+
TRUNCATE_APPEND_TEXT + content[content.length - length + TRUNCATE_APPEND_TEXT.length,
|
31
|
+
length - TRUNCATE_APPEND_TEXT.length]
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param length [Integer]
|
35
|
+
# @return [String]
|
36
|
+
def truncate_with_label(length = TRUNCATE_DEFAULT_LENGTH)
|
37
|
+
header = [label, file, pretty_file_size].join(' / ')
|
38
|
+
return ">>> #{header} (Not found) <<<" unless file.file?
|
39
|
+
|
40
|
+
content = truncate(length)
|
41
|
+
content.blank? ? ">>> #{header} (Blank) <<<" : ">>> #{header}\n#{content}\n<<< #{header}\n"
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
# @return [EacRubyUtils::Fs::Temp::File
|
47
|
+
def file_uncached
|
48
|
+
::EacRubyUtils::Fs::Temp.file
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/eac_fs/logs.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'eac_ruby_utils/fs/temp'
|
5
|
+
require 'filesize'
|
6
|
+
|
7
|
+
module EacFs
|
8
|
+
class Logs
|
9
|
+
require_sub __FILE__
|
10
|
+
|
11
|
+
# @param label [Symbol]
|
12
|
+
# @return [EacRubyUtils::Fs::Temp::File]
|
13
|
+
def [](label)
|
14
|
+
log_set.fetch(sanitize_label(label)).file
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param label [Symbol]
|
18
|
+
# @return [EacFs::Logs]
|
19
|
+
def add(label)
|
20
|
+
file = ::EacFs::Logs::File.new(sanitize_label(label))
|
21
|
+
log_set[file.label] = file
|
22
|
+
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [EacFs::Logs]
|
27
|
+
def remove_all
|
28
|
+
log_set.each_key { |label| remove(label) }
|
29
|
+
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param label [Symbol]
|
34
|
+
def remove(label)
|
35
|
+
log_set.fetch(sanitize_label(label)).remove
|
36
|
+
log_set.delete(sanitize_label(label))
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param length [Integer]
|
40
|
+
# @return [String]
|
41
|
+
def truncate_all(length = ::EacFs::Logs::File::TRUNCATE_DEFAULT_LENGTH)
|
42
|
+
"Files: #{log_set.length}\n" +
|
43
|
+
log_set.values.map { |file| file.truncate_with_label(length) }.join
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# @param label [Object]
|
49
|
+
# @return [Symbol]
|
50
|
+
def sanitize_label(label)
|
51
|
+
label.to_sym
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [Hash<Symbol, EacFs::Logs::File>]
|
55
|
+
def log_set
|
56
|
+
@log_set ||= {}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -5,10 +5,12 @@ require 'eac_fs/patches/module/fs_cache'
|
|
5
5
|
class Object
|
6
6
|
# @return [EacFs::Cache]
|
7
7
|
def fs_cache
|
8
|
-
|
8
|
+
oid = fs_cache_object_id
|
9
|
+
oid = [oid.to_s] unless oid.is_a?(::Enumerable)
|
10
|
+
oid.inject(self.class.fs_cache) { |a, e| a.child(e.to_s) }
|
9
11
|
end
|
10
12
|
|
11
|
-
# @return [String]
|
13
|
+
# @return [String, Array<String>]
|
12
14
|
def fs_cache_object_id
|
13
15
|
raise 'Abstract method hit'
|
14
16
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_fs/traverser'
|
4
|
+
|
5
|
+
module EacFs
|
6
|
+
module Traversable
|
7
|
+
PROP_METHOD_PREFIX = 'traverser_'
|
8
|
+
BOOLEAN_PROPS = %i[hidden_directories recursive sort].freeze
|
9
|
+
PATH_PROPS = %i[check_directory check_file].freeze
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def prop_method_name(prop)
|
13
|
+
"#{PROP_METHOD_PREFIX}#{prop}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
PATH_PROPS.each do |method|
|
18
|
+
define_method(::EacFs::Traversable.prop_method_name(method)) do |_path|
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
BOOLEAN_PROPS.each do |method|
|
24
|
+
define_method(::EacFs::Traversable.prop_method_name(method)) do
|
25
|
+
false
|
26
|
+
end
|
27
|
+
|
28
|
+
define_method("#{::EacFs::Traversable.prop_method_name(method)}?") do
|
29
|
+
send(method_name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def traverser_check_path(path)
|
34
|
+
traverser_new.check_path(path)
|
35
|
+
end
|
36
|
+
|
37
|
+
def traverser_new
|
38
|
+
r = ::EacFs::Traverser.new
|
39
|
+
(BOOLEAN_PROPS + PATH_PROPS).each do |prop|
|
40
|
+
r.send("#{prop}=", method(::EacFs::Traversable.prop_method_name(prop)))
|
41
|
+
end
|
42
|
+
r
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EacFs
|
4
|
+
class Traverser
|
5
|
+
attr_accessor :check_directory, :check_file, :recursive, :hidden_directories, :sort
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
options.each do |accessor, value|
|
9
|
+
send("#{accessor}=", value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def check_path(path)
|
14
|
+
path = ::Pathname.new(path.to_s) unless path.is_a?(::Pathname)
|
15
|
+
internal_check_path(path, 0)
|
16
|
+
end
|
17
|
+
|
18
|
+
def hidden_directories?
|
19
|
+
boolean_value(hidden_directories)
|
20
|
+
end
|
21
|
+
|
22
|
+
def recursive?
|
23
|
+
boolean_value(recursive)
|
24
|
+
end
|
25
|
+
|
26
|
+
def sort?
|
27
|
+
boolean_value(sort)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def boolean_value(source_value)
|
33
|
+
source_value = source_value.call if source_value.respond_to?(:call)
|
34
|
+
source_value ? true : false
|
35
|
+
end
|
36
|
+
|
37
|
+
def each_child(dir, &block)
|
38
|
+
if sort?
|
39
|
+
dir.each_child.sort_by { |p| [p.to_s] }.each(&block)
|
40
|
+
else
|
41
|
+
dir.each_child(&block)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def process_directory?(level)
|
46
|
+
level.zero? || recursive?
|
47
|
+
end
|
48
|
+
|
49
|
+
def inner_check_directory(dir, level)
|
50
|
+
return unless process_directory?(level)
|
51
|
+
|
52
|
+
user_check_directory(dir)
|
53
|
+
each_child(dir) do |e|
|
54
|
+
next unless !e.basename.to_s.start_with?('.') || hidden_directories?
|
55
|
+
|
56
|
+
internal_check_path(e, level + 1)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def internal_check_path(path, level)
|
61
|
+
if path.file?
|
62
|
+
user_check_file(path)
|
63
|
+
elsif path.directory?
|
64
|
+
inner_check_directory(path, level)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def user_check_file(path)
|
69
|
+
check_file&.call(path)
|
70
|
+
end
|
71
|
+
|
72
|
+
def user_check_directory(path)
|
73
|
+
check_directory&.call(path)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/lib/eac_fs/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eac_fs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Put here the authors
|
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: content-type
|
@@ -76,6 +76,8 @@ files:
|
|
76
76
|
- lib/eac_fs/cache.rb
|
77
77
|
- lib/eac_fs/cached_download.rb
|
78
78
|
- lib/eac_fs/file_info.rb
|
79
|
+
- lib/eac_fs/logs.rb
|
80
|
+
- lib/eac_fs/logs/file.rb
|
79
81
|
- lib/eac_fs/patches.rb
|
80
82
|
- lib/eac_fs/patches/module.rb
|
81
83
|
- lib/eac_fs/patches/module/fs_cache.rb
|
@@ -83,6 +85,8 @@ files:
|
|
83
85
|
- lib/eac_fs/patches/object/fs_cache.rb
|
84
86
|
- lib/eac_fs/patches/pathname.rb
|
85
87
|
- lib/eac_fs/patches/pathname/info.rb
|
88
|
+
- lib/eac_fs/traversable.rb
|
89
|
+
- lib/eac_fs/traverser.rb
|
86
90
|
- lib/eac_fs/version.rb
|
87
91
|
homepage:
|
88
92
|
licenses: []
|