tools 0.4.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -0
- data/Gemfile +1 -1
- data/Rakefile +9 -15
- data/aux2 +1 -5
- data/bin/tools +29 -37
- data/lib/files/requireds.rb +0 -2
- data/lib/lib/array.rb +45 -65
- data/lib/lib/cache.rb +11 -18
- data/lib/lib/config.rb +47 -76
- data/lib/lib/console.rb +92 -108
- data/lib/lib/display.rb +10 -29
- data/lib/lib/files.rb +26 -44
- data/lib/lib/hash.rb +21 -22
- data/lib/lib/log.rb +28 -32
- data/lib/lib/net.rb +58 -80
- data/lib/lib/object.rb +8 -7
- data/lib/lib/prompt.rb +24 -26
- data/lib/lib/string.rb +15 -15
- data/lib/lib/utils.rb +90 -66
- data/lib/tools.rb +3 -8
- data/lib/tools/version.rb +2 -2
- data/test/mini_array.rb +7 -12
- data/test/mini_cache.rb +9 -12
- data/test/mini_config.rb +21 -12
- data/test/mini_console.rb +6 -8
- data/test/mini_display.rb +6 -9
- data/test/mini_file.rb +20 -24
- data/test/mini_hash.rb +27 -38
- data/test/mini_log.rb +2 -4
- data/test/mini_net.rb +167 -185
- data/test/mini_object.rb +16 -21
- data/test/mini_prompt.rb +28 -16
- data/test/mini_string.rb +11 -14
- data/test/mini_utils.rb +13 -16
- data/test/run +9 -9
- data/tools.gemspec +12 -14
- metadata +35 -20
data/lib/lib/object.rb
CHANGED
@@ -3,33 +3,35 @@ class Object
|
|
3
3
|
#
|
4
4
|
# @return boolean
|
5
5
|
def boolean?
|
6
|
-
|
6
|
+
is_a?(TrueClass) || is_a?(FalseClass)
|
7
7
|
end
|
8
8
|
|
9
9
|
# Self test Trueclass.
|
10
10
|
#
|
11
11
|
# @return boolean
|
12
12
|
def true?
|
13
|
-
|
13
|
+
is_a?(TrueClass)
|
14
14
|
end
|
15
|
+
|
15
16
|
# Self test Falseclass.
|
16
17
|
#
|
17
18
|
# @return boolean
|
18
19
|
def false?
|
19
|
-
|
20
|
+
is_a?(FalseClass)
|
20
21
|
end
|
21
22
|
|
22
23
|
# Self test Symbol class.
|
23
24
|
#
|
24
25
|
# @return boolean
|
25
26
|
def symbol?
|
26
|
-
|
27
|
+
is_a?(Symbol)
|
27
28
|
end
|
29
|
+
|
28
30
|
# Self test String class.
|
29
31
|
#
|
30
32
|
# @return boolean
|
31
33
|
def string?
|
32
|
-
|
34
|
+
is_a?(String)
|
33
35
|
end
|
34
36
|
|
35
37
|
# Self test nil Object class.
|
@@ -38,5 +40,4 @@ class Object
|
|
38
40
|
# def nil?
|
39
41
|
# return '' if self == nil
|
40
42
|
# end
|
41
|
-
|
42
|
-
end
|
43
|
+
end
|
data/lib/lib/prompt.rb
CHANGED
@@ -2,53 +2,51 @@ require 'singleton'
|
|
2
2
|
class ToolsPrompt
|
3
3
|
include Singleton
|
4
4
|
|
5
|
-
def initialize(options = {})
|
6
|
-
end
|
5
|
+
def initialize(options = {}); end
|
7
6
|
|
8
|
-
def self.yes?
|
7
|
+
def self.yes?(*args)
|
9
8
|
prompt = TTY::Prompt.new
|
10
|
-
prompt.yes?
|
9
|
+
prompt.yes?(*args)
|
11
10
|
end
|
12
11
|
|
13
|
-
def self.no?
|
12
|
+
def self.no?(*args)
|
14
13
|
prompt = TTY::Prompt.new
|
15
|
-
prompt.no?
|
14
|
+
prompt.no?(*args)
|
16
15
|
end
|
17
16
|
|
18
|
-
def self.ask
|
17
|
+
def self.ask(*args)
|
19
18
|
prompt = TTY::Prompt.new
|
20
|
-
result = prompt.ask
|
21
|
-
|
19
|
+
result = prompt.ask(*args)
|
20
|
+
result
|
22
21
|
end
|
23
22
|
|
24
|
-
def self.mask
|
23
|
+
def self.mask(*args)
|
25
24
|
prompt = TTY::Prompt.new
|
26
|
-
result = prompt.mask
|
27
|
-
|
25
|
+
result = prompt.mask(*args)
|
26
|
+
result
|
28
27
|
end
|
29
28
|
|
30
|
-
def self.select
|
29
|
+
def self.select(*args)
|
31
30
|
prompt = TTY::Prompt.new
|
32
|
-
result = prompt.select
|
33
|
-
|
31
|
+
result = prompt.select(*args)
|
32
|
+
result
|
34
33
|
end
|
35
34
|
|
36
|
-
def self.multi_select
|
35
|
+
def self.multi_select(*args)
|
37
36
|
prompt = TTY::Prompt.new
|
38
|
-
result = prompt.multi_select
|
39
|
-
|
37
|
+
result = prompt.multi_select(*args)
|
38
|
+
result
|
40
39
|
end
|
41
40
|
|
42
|
-
def self.enum_select
|
41
|
+
def self.enum_select(*args)
|
43
42
|
prompt = TTY::Prompt.new
|
44
|
-
result = prompt.enum_select
|
45
|
-
|
43
|
+
result = prompt.enum_select(*args)
|
44
|
+
result
|
46
45
|
end
|
47
46
|
|
48
|
-
def self.expand
|
47
|
+
def self.expand(*args)
|
49
48
|
prompt = TTY::Prompt.new
|
50
|
-
result = prompt.expand
|
51
|
-
|
49
|
+
result = prompt.expand(*args)
|
50
|
+
result
|
52
51
|
end
|
53
|
-
|
54
|
-
end
|
52
|
+
end
|
data/lib/lib/string.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
class String
|
2
|
-
|
3
2
|
# Justity relative left or right position filled with a espefific char in String.
|
4
3
|
#
|
5
4
|
# sample:
|
@@ -10,12 +9,12 @@ class String
|
|
10
9
|
# @param size to justify.
|
11
10
|
# @param pattern pattern do justify
|
12
11
|
# @return formated string
|
13
|
-
def fix(size, pattern=' ')
|
12
|
+
def fix(size, pattern = ' ')
|
14
13
|
if size >= 0
|
15
14
|
self[0...size].rjust(size, pattern)
|
16
15
|
else
|
17
16
|
diff = size.abs - self.size
|
18
|
-
self + ''.fix(diff,pattern)
|
17
|
+
self + ''.fix(diff, pattern)
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
@@ -45,39 +44,40 @@ class String
|
|
45
44
|
#
|
46
45
|
# @return boolean
|
47
46
|
def numeric?
|
48
|
-
Float(self)
|
47
|
+
!Float(self).nil?
|
48
|
+
rescue StandardError
|
49
|
+
false
|
49
50
|
end
|
50
51
|
|
51
52
|
# Self test digits String class.
|
52
53
|
#
|
53
54
|
# @return boolean
|
54
55
|
def num?
|
55
|
-
|
56
|
+
!match(/^[[:digit:]]+$/).nil?
|
56
57
|
end
|
57
58
|
|
58
59
|
# Self test alphanum String class.
|
59
60
|
#
|
60
61
|
# @return boolean
|
61
62
|
def alnum?
|
62
|
-
|
63
|
+
!match(/^[[:alnum:]]+$/).nil?
|
63
64
|
end
|
64
65
|
|
65
66
|
# Self test alpha String class.
|
66
67
|
#
|
67
68
|
# @return boolean
|
68
69
|
def alpha?
|
69
|
-
|
70
|
+
!match(/^[[:alpha:]]+$/).nil?
|
70
71
|
end
|
71
72
|
|
72
73
|
def help?
|
73
|
-
if
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
74
|
+
if eql?('?') ||
|
75
|
+
eql?('-h') ||
|
76
|
+
eql?('--help') ||
|
77
|
+
eql?('help')
|
78
|
+
true
|
78
79
|
else
|
79
|
-
|
80
|
+
false
|
80
81
|
end
|
81
82
|
end
|
82
|
-
|
83
|
-
end
|
83
|
+
end
|
data/lib/lib/utils.rb
CHANGED
@@ -25,123 +25,147 @@ class ToolsUtil
|
|
25
25
|
# @param hash
|
26
26
|
# return hash symbolized
|
27
27
|
def self.symbolize_keys(hash)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
28
|
+
{}.tap do |h|
|
29
|
+
hash.each { |key, value| h[key.to_sym] = map_value(value) }
|
30
|
+
end
|
31
|
+
# hash.each_with_object({}) do |(key, value), result|
|
32
|
+
# new_key = case key
|
33
|
+
# when String then key.to_sym
|
34
|
+
# else key
|
35
|
+
# end
|
36
|
+
# new_value = case value
|
37
|
+
# when Hash then symbolize_keys(value)
|
38
|
+
# else value
|
39
|
+
# end
|
40
|
+
# result[new_key] = new_value
|
41
|
+
# end
|
40
42
|
end
|
41
43
|
|
42
|
-
|
44
|
+
def self.map_value(thing)
|
45
|
+
case thing
|
46
|
+
when Hash
|
47
|
+
symbolize_keys(thing)
|
48
|
+
when Array
|
49
|
+
thing.map { |v| map_value(v) }
|
50
|
+
else
|
51
|
+
thing
|
52
|
+
end
|
53
|
+
end
|
43
54
|
|
44
55
|
# Test a valid json string.
|
45
56
|
#
|
46
57
|
# @param source Json string to be tested
|
47
58
|
# @return boolean
|
48
|
-
def self.valid_json?
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
false
|
53
|
-
end
|
59
|
+
def self.valid_json?(source)
|
60
|
+
JSON.parse(source) && true
|
61
|
+
rescue JSON::ParserError
|
62
|
+
false
|
54
63
|
end
|
55
64
|
|
56
65
|
# Test a valid yaml string.
|
57
66
|
#
|
58
67
|
# @param source Yaml string to be tested
|
59
68
|
# @return boolean
|
60
|
-
def self.valid_yaml?
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
false
|
65
|
-
end
|
69
|
+
def self.valid_yaml?(source)
|
70
|
+
YAML.parse(source) && true
|
71
|
+
rescue Psych::SyntaxError
|
72
|
+
false
|
66
73
|
end
|
67
74
|
|
68
75
|
# Return a formated DateTime.
|
69
76
|
#
|
70
77
|
# @param format with a DateTime format default are: 12 Outubro 2018, 08:29
|
71
78
|
# @return [String] formated date
|
72
|
-
def self.get_date
|
73
|
-
|
79
|
+
def self.get_date(format = '%e %B %Y, %H:%M')
|
80
|
+
I18n.l(DateTime.now, format: format)
|
74
81
|
end
|
75
82
|
|
76
|
-
|
77
83
|
# Modify the class variable.. String => change value, Hash => merge, Array => insert
|
78
84
|
#
|
79
85
|
# @param variable name variable
|
80
86
|
# @param value value veriable
|
81
87
|
# @return []
|
82
|
-
def self.set_variable_ext
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
ToolsDisplay.show "\tToolsDisplay error [set_variable_ext]. Attempt insert #{variable.class} into Hash variable....".light_red
|
94
|
-
end
|
95
|
-
when 'Array'
|
96
|
-
aux.insert(-1,value)
|
97
|
-
self.set_variable variable, aux
|
98
|
-
end
|
88
|
+
def self.set_variable_ext(variable, value)
|
89
|
+
return unless instance_variable_defined? "@#{variable}"
|
90
|
+
|
91
|
+
aux = get_variable variable
|
92
|
+
case aux.class.to_s
|
93
|
+
when 'String'
|
94
|
+
set_string_variable variable, value
|
95
|
+
when 'Hash'
|
96
|
+
set_hash_variable variable, value, aux
|
97
|
+
when 'Array'
|
98
|
+
set_array_variable variable, value, aux
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
|
+
def self.set_string_variable(variable, value)
|
103
|
+
set_variable variable, value
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.set_hash_variable(variable, value, aux)
|
107
|
+
aux.merge! value
|
108
|
+
set_variable variable, aux
|
109
|
+
rescue StandardError
|
110
|
+
ToolsDisplay.show "\tToolsDisplay error [set_variable_ext].".light_red
|
111
|
+
ToolsDisplay.show "\tAttempt insert #{variable.class} into Hash variable....".light_red
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.set_array_variable(variable, value, aux)
|
115
|
+
aux.insert(-1, value)
|
116
|
+
set_variable variable, aux
|
117
|
+
end
|
118
|
+
|
102
119
|
# Set a new class variable.
|
103
120
|
#
|
104
121
|
# @param variable variable name to set
|
105
122
|
# @param value value for variable
|
106
123
|
# @return []
|
107
|
-
def self.set_variable
|
108
|
-
|
124
|
+
def self.set_variable(variable, value)
|
125
|
+
instance_variable_set("@#{variable}", value)
|
109
126
|
end
|
110
127
|
|
111
128
|
# Get a existent class variable.
|
112
129
|
#
|
113
130
|
# @param variable variable name to retrive
|
114
131
|
# @return variable value
|
115
|
-
def self.get_variable
|
116
|
-
|
132
|
+
def self.get_variable(variable)
|
133
|
+
instance_variable_get("@#{variable}")
|
117
134
|
end
|
118
135
|
|
119
136
|
# Get all existent class variablea.
|
120
137
|
#
|
121
138
|
# @return variables
|
122
139
|
def self.get_variables
|
123
|
-
|
140
|
+
instance_variables
|
124
141
|
end
|
125
142
|
|
126
143
|
# Return a plain text for content of String or Hash or Array.
|
127
144
|
#
|
128
145
|
# @param value Content of variable to translate to Plaint text
|
129
146
|
# @return plain text content
|
130
|
-
def self.get_plain_text
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
end
|
147
|
+
def self.get_plain_text(value)
|
148
|
+
case value
|
149
|
+
when String then "\t#{value.yellow}"
|
150
|
+
when Hash, Array
|
151
|
+
# old_stdout = $stdout
|
152
|
+
# captured_stdio = StringIO.new('', 'w')
|
153
|
+
# $stdout = captured_stdio
|
154
|
+
# ap value, plain: true
|
155
|
+
# $stdout = old_stdout
|
156
|
+
# captured_stdio.string
|
157
|
+
get_plain_text_if_hash_or_array value
|
158
|
+
else
|
159
|
+
value
|
160
|
+
end
|
145
161
|
end
|
146
162
|
|
147
|
-
|
163
|
+
def self.get_plain_text_if_hash_or_array(value)
|
164
|
+
old_stdout = $stdout
|
165
|
+
captured_stdio = StringIO.new('', 'w')
|
166
|
+
$stdout = captured_stdio
|
167
|
+
ap value, plain: true
|
168
|
+
$stdout = old_stdout
|
169
|
+
captured_stdio.string
|
170
|
+
end
|
171
|
+
end
|
data/lib/tools.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'tools/version'
|
2
2
|
|
3
3
|
# Basic models
|
4
4
|
require 'lib/utils'
|
@@ -18,6 +18,7 @@ require 'lib/prompt'
|
|
18
18
|
require 'lib/console'
|
19
19
|
|
20
20
|
load __dir__ + '/files/requireds.rb'
|
21
|
+
include Dnsruby
|
21
22
|
|
22
23
|
module Tools
|
23
24
|
|
@@ -32,10 +33,8 @@ module Tools
|
|
32
33
|
self.configuration ||= Configuration.new
|
33
34
|
yield(configuration)
|
34
35
|
end
|
35
|
-
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
38
|
def self.root
|
40
39
|
File.dirname __dir__
|
41
40
|
end
|
@@ -71,15 +70,11 @@ module Tools
|
|
71
70
|
def self.gem_path
|
72
71
|
ENV['GEM_PATH']
|
73
72
|
end
|
74
|
-
|
75
73
|
end
|
76
74
|
|
77
75
|
Tools.configure do |config|
|
78
76
|
config.console_prompt = Tools::VERSION
|
79
77
|
I18n.load_path = Dir[Tools.files + '/pt-BR.yml']
|
80
78
|
I18n.locale = 'pt-BR'.to_sym
|
81
|
-
|
79
|
+
# ToolsUtil.instance
|
82
80
|
end
|
83
|
-
|
84
|
-
|
85
|
-
|