avm-files 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ae0d20f3f31743dfbf25f94dd24f36dc97dce93da94377fc9f85742b10d3e34
4
- data.tar.gz: 23ed109ef8b2691f7b14fdaeaab8e194d4ff8a80739130c043aa109988f57e98
3
+ metadata.gz: 7eb4bae1712c9112736c6049c46a5e54df49e9e65bb51ab1570385d9266f3b24
4
+ data.tar.gz: 374f7866ac01d7fe1b21867b5ef5f065734a339b9a7bf231d632d90139fa8fcf
5
5
  SHA512:
6
- metadata.gz: '085fd89600d3d0cdda2f4cc731eb14380913c578d24d75c2e82acafe690d7600111087622a6e94aad5707dfa0c8f3bb3735f64ad4a364ca356d49390cf590de0'
7
- data.tar.gz: b04360ba65b34e9e0678273127b74e1ae5aef1b0a55844ce965c55407e86695ac80b42d395586b410778eb766bceb211f6d59c22df4b89a94d31535c82736dad
6
+ metadata.gz: 8ea039168b8d9c83421017bc49bd368af864763e704d8965e24fd216c8e670568e640b46c54a48b07f027c0b7d040197d92727f2eb22d51c58ff1028e23f1bdc
7
+ data.tar.gz: 79057243c6e31f617403edb70e4d1a6e8c1ef932501f26feca2ee9f9abef25171aa93edd78cf4a7ef2356c7c38dee0b942eda33989f9dc97f47914a35a8c6626
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Avm
4
4
  module Files
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avm-files
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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-13 00:00:00.000000000 Z
11
+ date: 2021-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avm
@@ -107,7 +107,6 @@ files:
107
107
  - lib/avm/files/formatter/formats/ruby.rb
108
108
  - lib/avm/files/formatter/formats/xml.rb
109
109
  - lib/avm/files/formatter/utf8_assert.rb
110
- - lib/avm/files/rotate.rb
111
110
  - lib/avm/files/version.rb
112
111
  homepage:
113
112
  licenses: []
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/core_ext/object/blank'
4
- require 'eac_ruby_utils/simple_cache'
5
-
6
- module Avm
7
- module Files
8
- class Rotate
9
- include ::EacRubyUtils::SimpleCache
10
-
11
- attr_reader :options, :source_path
12
-
13
- def initialize(source_path, options = {})
14
- @source_path = source_path
15
- @options = options
16
- end
17
-
18
- def run
19
- validate_msg = validate
20
- return validate_msg if validate_msg.present?
21
-
22
- ::FileUtils.mv(source_path, target_path)
23
- check_space_limit
24
- nil
25
- end
26
-
27
- def space_limit
28
- r = options[:space_limit].try(:to_i)
29
- return r if r.present? && r.positive?
30
-
31
- r
32
- end
33
-
34
- def space_used
35
- rotated_files.inject(0) { |a, e| a + ::File.size(e) }
36
- end
37
-
38
- def rotated_files
39
- ::Dir["#{dirname}/#{source_basename_without_extension}*#{source_extension}"]
40
- end
41
-
42
- def oldest_rotated_file
43
- rotated_files.min_by { |file| [::File.mtime(file)] }
44
- end
45
-
46
- private
47
-
48
- def check_space_limit
49
- return unless space_limit
50
-
51
- while space_used > space_limit
52
- file = oldest_rotated_file
53
- unless file
54
- raise 'oldest_rotated_file returned nil ' \
55
- "(Limit: #{space_limit}, used: #{space_used})"
56
- end
57
- ::File.delete(file)
58
- end
59
- end
60
-
61
- def validate
62
- return "Source file \"#{source_path}\" does not exist" unless ::File.exist?(source_path)
63
- return "File \"#{source_path}\" is already rotated" if source_rotated?
64
-
65
- nil
66
- end
67
-
68
- def source_rotated?
69
- source_basename_without_extension.match(/[PN]\d{4}\z/)
70
- end
71
-
72
- def source_extension_uncached
73
- file_extension(::File.basename(source_path))
74
- end
75
-
76
- def source_basename_without_extension
77
- ::File.basename(source_path, source_extension)
78
- end
79
-
80
- def target_path_uncached
81
- ::File.join(dirname, target_basename)
82
- end
83
-
84
- def dirname
85
- ::File.dirname(source_path)
86
- end
87
-
88
- def target_basename
89
- source_basename_without_extension + target_suffix + source_extension
90
- end
91
-
92
- def target_suffix
93
- return '_UNKNOWN_MTIME' unless ::File.exist?(source_path)
94
-
95
- t = ::File.mtime(source_path)
96
- t.strftime('_%Y-%m-%d_%H-%M-%S_') + t.strftime('%z').gsub(/\A\+/, 'P').gsub(/\A\-/, 'N')
97
- end
98
-
99
- def file_extension(basename)
100
- extension = ::File.extname(basename)
101
- return '' if extension.blank?
102
-
103
- file_extension(::File.basename(basename, extension)) + extension
104
- end
105
- end
106
- end
107
- end