eac_fs 0.4.0 → 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f84f3e6bee2753e30079a3e79cf4d8ad3568b59dfeac360f1de73d2ca34162d5
4
- data.tar.gz: 184ab1296d60754f52dc2d598dec9b18871b1024dde617b3b2cac3d8b50c1490
3
+ metadata.gz: fcb1c3229305e0ad12c014a54395563e8550d6f140c73e59de2af545e2390e4f
4
+ data.tar.gz: 4b507f0c4f589b3d686ed93c5de67cba5504d7e73d539e6758ccf1988f443132
5
5
  SHA512:
6
- metadata.gz: 6c1b1a320116205e2c690564bdd3d6aa78451a0b26ef3feeb8a5cddf76b6a96e427e89bc5db02fd535b10453b61d01edd6211e075a2e9819b9f41bef809d1203
7
- data.tar.gz: f4bc08f798e701a06a6dadf703eeb6d0c4c5a21c25cb95c5946b6aaf40ae13f9a015a8cb8ac6ffaac96abffefe71dc25e2bf9f6a8970bb2a149ce552306ab4f6
6
+ metadata.gz: b2e9da3106b51158639edea495810817dae7e5884d28092b2fd7da4b20990c2d1679dae6d4b8f1765fa28414c8a43a016ee97e91a31a2fc21aa296235d5d99fb
7
+ data.tar.gz: 679acb700750d0c560b458a4385a41ff26728534f5a92b00885b1d2308f48a1246fb23b510c64e85d99f76c31ae32eed65ad13b225efb5edec7990fd9ebc5c1f
@@ -0,0 +1,50 @@
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
+ def file_size
15
+ file.file? ? file.size : 0
16
+ end
17
+
18
+ def pretty_file_size
19
+ ::Filesize.from("#{file_size} B").pretty
20
+ end
21
+
22
+ # @param length [Integer]
23
+ # @return [String]
24
+ def truncate(length = TRUNCATE_DEFAULT_LENGTH)
25
+ content = file.file? ? file.read.strip : ''
26
+ return content if content.length <= TRUNCATE_DEFAULT_LENGTH
27
+
28
+ TRUNCATE_APPEND_TEXT + content[content.length - length + TRUNCATE_APPEND_TEXT.length,
29
+ length - TRUNCATE_APPEND_TEXT.length]
30
+ end
31
+
32
+ # @param length [Integer]
33
+ # @return [String]
34
+ def truncate_with_label(length = TRUNCATE_DEFAULT_LENGTH)
35
+ header = [label, file, pretty_file_size].join(' / ')
36
+ return ">>> #{header} (Not found) <<<" unless file.file?
37
+
38
+ content = truncate(length)
39
+ content.blank? ? ">>> #{header} (Blank) <<<" : ">>> #{header}\n#{content}\n<<< #{header}\n"
40
+ end
41
+
42
+ protected
43
+
44
+ # @return [EacRubyUtils::Fs::Temp::File
45
+ def file_uncached
46
+ ::EacRubyUtils::Fs::Temp.file
47
+ end
48
+ end
49
+ end
50
+ end
@@ -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,6 +5,13 @@ require 'eac_fs/patches/module/fs_cache'
5
5
  class Object
6
6
  # @return [EacFs::Cache]
7
7
  def fs_cache
8
- self.class.fs_cache
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) }
11
+ end
12
+
13
+ # @return [String, Array<String>]
14
+ def fs_cache_object_id
15
+ raise 'Abstract method hit'
9
16
  end
10
17
  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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacFs
4
- VERSION = '0.4.0'
4
+ VERSION = '0.8.0'
5
5
  end
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.0
4
+ version: 0.8.0
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-09-25 00:00:00.000000000 Z
11
+ date: 2021-12-06 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: []