eac_ruby_utils 0.30.0 → 0.31.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 +4 -4
- 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/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 +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 278662c9d15695f7556fa82d0042a29c65cf38490ef0eb69ba39bb06e125f913
|
4
|
+
data.tar.gz: 0f4aa577af44969f94b5a46e3be4c394fd378449ea6e76d5deacfaa2f480b224
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bca74df52d4266c93b2d9355fa788c805c526145ad324e0852605f96f2e749cd15b3db6f2050f07cb30f0e890107e63294b8053415f8384008654084baf7529b
|
7
|
+
data.tar.gz: 817d006273a79b5f18d0346e7fbb8c21ad0c15fb53db36a9deb040270ea76b78caa8674b68eab81911a56e6eb8e24e02cef83a93809a17233120fb162a1e344a
|
@@ -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.31.0
|
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-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -135,6 +135,8 @@ files:
|
|
135
135
|
- lib/eac_ruby_utils/filesystem_cache.rb
|
136
136
|
- lib/eac_ruby_utils/fs.rb
|
137
137
|
- lib/eac_ruby_utils/fs/temp.rb
|
138
|
+
- lib/eac_ruby_utils/fs/temp/directory.rb
|
139
|
+
- lib/eac_ruby_utils/fs/temp/file.rb
|
138
140
|
- lib/eac_ruby_utils/fs/traversable.rb
|
139
141
|
- lib/eac_ruby_utils/fs/traverser.rb
|
140
142
|
- lib/eac_ruby_utils/fs_cache.rb
|
@@ -152,6 +154,8 @@ files:
|
|
152
154
|
- lib/eac_ruby_utils/patches.rb
|
153
155
|
- lib/eac_ruby_utils/patches/class.rb
|
154
156
|
- lib/eac_ruby_utils/patches/class/common_constructor.rb
|
157
|
+
- lib/eac_ruby_utils/patches/enumerable.rb
|
158
|
+
- lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
|
155
159
|
- lib/eac_ruby_utils/patches/hash.rb
|
156
160
|
- lib/eac_ruby_utils/patches/hash/options_consumer.rb
|
157
161
|
- lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb
|
@@ -168,6 +172,11 @@ files:
|
|
168
172
|
- lib/eac_ruby_utils/patches/object/to_pathname.rb
|
169
173
|
- lib/eac_ruby_utils/patches/pathname.rb
|
170
174
|
- lib/eac_ruby_utils/patches/pathname/basename_sub.rb
|
175
|
+
- lib/eac_ruby_utils/patches/regexp.rb
|
176
|
+
- lib/eac_ruby_utils/patches/regexp/if_match.rb
|
177
|
+
- lib/eac_ruby_utils/patches/time.rb
|
178
|
+
- lib/eac_ruby_utils/patches/time/default_time_zone_set.rb
|
179
|
+
- lib/eac_ruby_utils/patches/time/local_time_zone.rb
|
171
180
|
- lib/eac_ruby_utils/paths_hash.rb
|
172
181
|
- lib/eac_ruby_utils/require_sub.rb
|
173
182
|
- lib/eac_ruby_utils/rspec.rb
|