eac_ruby_utils 0.30.0 → 0.33.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_ruby_utils/envs/executable.rb +2 -2
- data/lib/eac_ruby_utils/fs/extname.rb +30 -0
- data/lib/eac_ruby_utils/fs/temp.rb +27 -8
- data/lib/eac_ruby_utils/fs/temp/directory.rb +16 -0
- data/lib/eac_ruby_utils/fs/temp/file.rb +34 -0
- data/lib/eac_ruby_utils/patches/enumerable.rb +4 -0
- data/lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb +45 -0
- data/lib/eac_ruby_utils/patches/module/patch.rb +1 -1
- data/lib/eac_ruby_utils/patches/regexp.rb +4 -0
- data/lib/eac_ruby_utils/patches/regexp/if_match.rb +16 -0
- data/lib/eac_ruby_utils/patches/time.rb +4 -0
- data/lib/eac_ruby_utils/patches/time/default_time_zone_set.rb +5 -0
- data/lib/eac_ruby_utils/patches/time/local_time_zone.rb +25 -0
- data/lib/eac_ruby_utils/version.rb +1 -1
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b15ce47fc0a4be7f0d4d5c508e7460447818bcc4eef4f1b49d70c21475320091
|
4
|
+
data.tar.gz: 904f3e7fddda7113e1b70decadd20e9d1fd0e36c5a2f5467067a76abc1680ee1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24b859d02cbb17c2a87f89fecfac6d066cc3446516ecb34dc92d56bbc403a3969229e59622e0c654daf816585b539d14bed37400a072cd63fdf67160804e9d42
|
7
|
+
data.tar.gz: b02dc4a1d20787d3cbc0de8a0831d6c0cda42a5f2a6c7af3ce6fb7b6e85e65fd93ef9253d889297c034f8452e867735a8b9a4e5f1cbcd8c49028f4ee26f1a201
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EacRubyUtils
|
4
|
+
module Fs
|
5
|
+
class << self
|
6
|
+
# A [File.extname] which find multiple extensions (Ex.: .tar.gz).
|
7
|
+
def extname(path, limit = -1)
|
8
|
+
recursive_extension(::File.basename(path), limit)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Shortcut to +extname(2)+.
|
12
|
+
def extname2(path)
|
13
|
+
extname(path, 2)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def recursive_extension(basename, limit)
|
19
|
+
return '' if limit.zero?
|
20
|
+
|
21
|
+
m = /\A(.+)(\.[a-z][a-z0-9]*)\z/i.match(basename)
|
22
|
+
if m
|
23
|
+
"#{recursive_extension(m[1], limit - 1)}#{m[2]}"
|
24
|
+
else
|
25
|
+
''
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'eac_ruby_utils/require_sub'
|
3
4
|
require 'pathname'
|
4
5
|
require 'tempfile'
|
5
6
|
|
@@ -8,23 +9,41 @@ module EacRubyUtils
|
|
8
9
|
# Utilities for temporary files.
|
9
10
|
module Temp
|
10
11
|
class << self
|
12
|
+
::EacRubyUtils.require_sub __FILE__
|
13
|
+
|
14
|
+
# Shortcut to +EacRubyUtils::Fs::Temp::Directory.new(*tempfile_args)+.
|
15
|
+
#
|
16
|
+
# @return [Pathname]
|
17
|
+
def directory(*tempfile_args)
|
18
|
+
::EacRubyUtils::Fs::Temp::Directory.new(*tempfile_args)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Shortcut to +EacRubyUtils::Fs::Temp::File.new(*tempfile_args)+.
|
22
|
+
#
|
11
23
|
# @return [Pathname]
|
12
24
|
def file(*tempfile_args)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
25
|
+
::EacRubyUtils::Fs::Temp::File.new(*tempfile_args)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Run a block while a temporary directory pathname is provided. The directory is deleted
|
29
|
+
# when the block is finished.
|
30
|
+
def on_directory(*tempfile_args)
|
31
|
+
temp_dir = directory(*tempfile_args)
|
32
|
+
begin
|
33
|
+
yield(temp_dir)
|
34
|
+
ensure
|
35
|
+
temp_dir.remove
|
36
|
+
end
|
18
37
|
end
|
19
38
|
|
20
39
|
# Run a block while a temporary file pathname is providade. The file is deleted when block
|
21
40
|
# is finished.
|
22
41
|
def on_file(*tempfile_args)
|
23
|
-
|
42
|
+
temp_file = file(*tempfile_args)
|
24
43
|
begin
|
25
|
-
yield(
|
44
|
+
yield(temp_file)
|
26
45
|
ensure
|
27
|
-
|
46
|
+
temp_file.remove
|
28
47
|
end
|
29
48
|
end
|
30
49
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/fs/temp/file'
|
4
|
+
|
5
|
+
module EacRubyUtils
|
6
|
+
module Fs
|
7
|
+
module Temp
|
8
|
+
class Directory < ::EacRubyUtils::Fs::Temp::File
|
9
|
+
def initialize(*tempfile_args)
|
10
|
+
super(*tempfile_args)
|
11
|
+
mkpath
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
module EacRubyUtils
|
7
|
+
module Fs
|
8
|
+
module Temp
|
9
|
+
class File < Pathname
|
10
|
+
# Temporary file
|
11
|
+
def initialize(*tempfile_args)
|
12
|
+
file = Tempfile.new(*tempfile_args)
|
13
|
+
path = file.path
|
14
|
+
file.close
|
15
|
+
file.unlink
|
16
|
+
super(path)
|
17
|
+
end
|
18
|
+
|
19
|
+
def remove
|
20
|
+
if directory?
|
21
|
+
rmtree
|
22
|
+
elsif file?
|
23
|
+
unlink
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove!
|
28
|
+
remove
|
29
|
+
raise "Tried to remove \"#{self}\", but it yet exists" if exist?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
# Produces a array with values's all combinations.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# %i[a b].boolean_combinations
|
8
|
+
# => [[], [:a], [:b], [:a, :b]]
|
9
|
+
#
|
10
|
+
# @return [Array]
|
11
|
+
def bool_array_combs
|
12
|
+
bool_combs([], method(:bool_array_combs_new_comb))
|
13
|
+
end
|
14
|
+
|
15
|
+
# Produces a hash with values's all combinations.
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# %i[a b].boolean_combinations
|
19
|
+
# => [{a: false, b: false}, {a: false, b: true}, {a: true, b: false}, {a: true, b: true}]
|
20
|
+
#
|
21
|
+
# @return [Hash]
|
22
|
+
def bool_hash_combs
|
23
|
+
bool_combs({}, method(:bool_hash_combs_new_comb))
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def bool_combs(empty_value, new_comb_method)
|
29
|
+
head = [empty_value]
|
30
|
+
r = inject(head) do |a, value|
|
31
|
+
new_comb_method.call(value, a)
|
32
|
+
end
|
33
|
+
r == head ? [] : r
|
34
|
+
end
|
35
|
+
|
36
|
+
def bool_array_combs_new_comb(value, combs)
|
37
|
+
combs + combs.map { |c| c + [value] }
|
38
|
+
end
|
39
|
+
|
40
|
+
def bool_hash_combs_new_comb(value, combs)
|
41
|
+
combs.flat_map do |comb|
|
42
|
+
[false, true].map { |bool_value| comb.dup.merge(value => bool_value) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Regexp
|
4
|
+
# If +self+ matches +string+ returns +block.call(Match result) or only Match result if block is
|
5
|
+
# not provided.
|
6
|
+
# If +self+ does not match +string+ raises a +ArgumentError+ if +required+ is truthy or return
|
7
|
+
# +nil+ otherwise.
|
8
|
+
def if_match(string, required = true, &block)
|
9
|
+
m = match(string)
|
10
|
+
if m
|
11
|
+
block ? block.call(m) : m
|
12
|
+
elsif required
|
13
|
+
raise(::ArgumentError, "Pattern \"#{self}\" does not match string \"#{string}\"")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/time/zones'
|
4
|
+
require 'eac_ruby_utils/envs'
|
5
|
+
|
6
|
+
class Time
|
7
|
+
class << self
|
8
|
+
TIMEDATECTL_TIMEZONE_LINE_PATTERN = %r{\s*Time zone:\s*(\S+/\S+)\s}.freeze
|
9
|
+
|
10
|
+
def local_time_zone
|
11
|
+
local_time_zone_by_timedatectl || local_time_zone_by_offset
|
12
|
+
end
|
13
|
+
|
14
|
+
def local_time_zone_by_timedatectl
|
15
|
+
executable = ::EacRubyUtils::Envs.local.executable('timedatectl', '--version')
|
16
|
+
return nil unless executable.exist?
|
17
|
+
|
18
|
+
TIMEDATECTL_TIMEZONE_LINE_PATTERN.if_match(executable.command.execute!) { |m| m[1] }
|
19
|
+
end
|
20
|
+
|
21
|
+
def local_time_zone_by_offset
|
22
|
+
::ActiveSupport::TimeZone[::Time.now.getlocal.gmt_offset].name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
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.33.1
|
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: 2020-
|
11
|
+
date: 2020-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -87,6 +87,9 @@ dependencies:
|
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0.1'
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 0.1.1
|
90
93
|
type: :development
|
91
94
|
prerelease: false
|
92
95
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -94,6 +97,9 @@ dependencies:
|
|
94
97
|
- - "~>"
|
95
98
|
- !ruby/object:Gem::Version
|
96
99
|
version: '0.1'
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 0.1.1
|
97
103
|
description:
|
98
104
|
email:
|
99
105
|
executables: []
|
@@ -134,7 +140,10 @@ files:
|
|
134
140
|
- lib/eac_ruby_utils/envs/ssh_env.rb
|
135
141
|
- lib/eac_ruby_utils/filesystem_cache.rb
|
136
142
|
- lib/eac_ruby_utils/fs.rb
|
143
|
+
- lib/eac_ruby_utils/fs/extname.rb
|
137
144
|
- lib/eac_ruby_utils/fs/temp.rb
|
145
|
+
- lib/eac_ruby_utils/fs/temp/directory.rb
|
146
|
+
- lib/eac_ruby_utils/fs/temp/file.rb
|
138
147
|
- lib/eac_ruby_utils/fs/traversable.rb
|
139
148
|
- lib/eac_ruby_utils/fs/traverser.rb
|
140
149
|
- lib/eac_ruby_utils/fs_cache.rb
|
@@ -152,6 +161,8 @@ files:
|
|
152
161
|
- lib/eac_ruby_utils/patches.rb
|
153
162
|
- lib/eac_ruby_utils/patches/class.rb
|
154
163
|
- lib/eac_ruby_utils/patches/class/common_constructor.rb
|
164
|
+
- lib/eac_ruby_utils/patches/enumerable.rb
|
165
|
+
- lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
|
155
166
|
- lib/eac_ruby_utils/patches/hash.rb
|
156
167
|
- lib/eac_ruby_utils/patches/hash/options_consumer.rb
|
157
168
|
- lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb
|
@@ -168,6 +179,11 @@ files:
|
|
168
179
|
- lib/eac_ruby_utils/patches/object/to_pathname.rb
|
169
180
|
- lib/eac_ruby_utils/patches/pathname.rb
|
170
181
|
- lib/eac_ruby_utils/patches/pathname/basename_sub.rb
|
182
|
+
- lib/eac_ruby_utils/patches/regexp.rb
|
183
|
+
- lib/eac_ruby_utils/patches/regexp/if_match.rb
|
184
|
+
- lib/eac_ruby_utils/patches/time.rb
|
185
|
+
- lib/eac_ruby_utils/patches/time/default_time_zone_set.rb
|
186
|
+
- lib/eac_ruby_utils/patches/time/local_time_zone.rb
|
171
187
|
- lib/eac_ruby_utils/paths_hash.rb
|
172
188
|
- lib/eac_ruby_utils/require_sub.rb
|
173
189
|
- lib/eac_ruby_utils/rspec.rb
|