eac_fs 0.6.0 → 0.9.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: edf014df44c45cd3a4e083d632141bcdbeb28f28d98f7759edd031d66acfb5ab
4
- data.tar.gz: d014be25611b6d58dc561c790c19f82ed19bad2c94002e19b922db52e1d18632
3
+ metadata.gz: 8adcfc3c8dd15da3828dbb522be9117d6c07c763006301dc248107fbe079356c
4
+ data.tar.gz: 358cecbb08b7c445f84147a3cbd1961fb5248ddf3b1dc3d771153440df5b1ad3
5
5
  SHA512:
6
- metadata.gz: 764614f228e5fa7eec4239459d839157352198eae43c10cb6580199e87879a595c469f29be6d677e514bf4e69cc42ce94ed8ac643e2acec546aa5d69750ac67e
7
- data.tar.gz: 7ab8466b22bda77686d8f372eb722bd26631fac807e3439a76ba93bfa6f992d699e0a80021dbe84475414d31e6c60424b0c8694fdc1c3902d10224d73929322d
6
+ metadata.gz: f532f36fc63a421c9c0bc9472014b13c545b55e8c27ca30db85935c9e90a1360bf4193edbfbe87b301c02c9713cea7ab55e1909923741bd7c3431c7f69a883ad
7
+ data.tar.gz: 1b32848d04f515f8bead2fd493f411fbfc99181b1927475cf9ace1a4aec2b6c2413e470217c3c1fb8e1504912559328eaec5586549c4d2fe93cbdc22241cde6f
@@ -0,0 +1,56 @@
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 clean
17
+ file.truncate(0) if file.exist?
18
+ end
19
+
20
+ def file_size
21
+ file.file? ? file.size : 0
22
+ end
23
+
24
+ def pretty_file_size
25
+ ::Filesize.from("#{file_size} B").pretty
26
+ end
27
+
28
+ # @param length [Integer]
29
+ # @return [String]
30
+ def truncate(length = TRUNCATE_DEFAULT_LENGTH)
31
+ content = file.file? ? file.read.strip : ''
32
+ return content if content.length <= TRUNCATE_DEFAULT_LENGTH
33
+
34
+ TRUNCATE_APPEND_TEXT + content[content.length - length + TRUNCATE_APPEND_TEXT.length,
35
+ length - TRUNCATE_APPEND_TEXT.length]
36
+ end
37
+
38
+ # @param length [Integer]
39
+ # @return [String]
40
+ def truncate_with_label(length = TRUNCATE_DEFAULT_LENGTH)
41
+ header = [label, file, pretty_file_size].join(' / ')
42
+ return ">>> #{header} (Not found) <<<" unless file.file?
43
+
44
+ content = truncate(length)
45
+ content.blank? ? ">>> #{header} (Blank) <<<" : ">>> #{header}\n#{content}\n<<< #{header}\n"
46
+ end
47
+
48
+ protected
49
+
50
+ # @return [EacRubyUtils::Fs::Temp::File
51
+ def file_uncached
52
+ ::EacRubyUtils::Fs::Temp.file
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,64 @@
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 clean_all
28
+ log_set.values.each(&:clean)
29
+ end
30
+
31
+ # @return [EacFs::Logs]
32
+ def remove_all
33
+ log_set.each_key { |label| remove(label) }
34
+
35
+ self
36
+ end
37
+
38
+ # @param label [Symbol]
39
+ def remove(label)
40
+ log_set.fetch(sanitize_label(label)).remove
41
+ log_set.delete(sanitize_label(label))
42
+ end
43
+
44
+ # @param length [Integer]
45
+ # @return [String]
46
+ def truncate_all(length = ::EacFs::Logs::File::TRUNCATE_DEFAULT_LENGTH)
47
+ "Files: #{log_set.length}\n" +
48
+ log_set.values.map { |file| file.truncate_with_label(length) }.join
49
+ end
50
+
51
+ private
52
+
53
+ # @param label [Object]
54
+ # @return [Symbol]
55
+ def sanitize_label(label)
56
+ label.to_sym
57
+ end
58
+
59
+ # @return [Hash<Symbol, EacFs::Logs::File>]
60
+ def log_set
61
+ @log_set ||= {}
62
+ end
63
+ end
64
+ 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.6.0'
4
+ VERSION = '0.9.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.6.0
4
+ version: 0.9.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-10-15 00:00:00.000000000 Z
11
+ date: 2021-12-11 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: []