tools 0.4.5 → 0.4.6

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.
@@ -3,33 +3,35 @@ class Object
3
3
  #
4
4
  # @return boolean
5
5
  def boolean?
6
- self.is_a?(TrueClass) || self.is_a?(FalseClass)
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
- self.is_a?(TrueClass)
13
+ is_a?(TrueClass)
14
14
  end
15
+
15
16
  # Self test Falseclass.
16
17
  #
17
18
  # @return boolean
18
19
  def false?
19
- self.is_a?(FalseClass)
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
- self.is_a?(Symbol)
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
- self.is_a?(String)
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
@@ -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? *args
7
+ def self.yes?(*args)
9
8
  prompt = TTY::Prompt.new
10
- prompt.yes? *args
9
+ prompt.yes?(*args)
11
10
  end
12
11
 
13
- def self.no? *args
12
+ def self.no?(*args)
14
13
  prompt = TTY::Prompt.new
15
- prompt.no? *args
14
+ prompt.no?(*args)
16
15
  end
17
16
 
18
- def self.ask *args
17
+ def self.ask(*args)
19
18
  prompt = TTY::Prompt.new
20
- result = prompt.ask *args
21
- return result
19
+ result = prompt.ask(*args)
20
+ result
22
21
  end
23
22
 
24
- def self.mask *args
23
+ def self.mask(*args)
25
24
  prompt = TTY::Prompt.new
26
- result = prompt.mask *args
27
- return result
25
+ result = prompt.mask(*args)
26
+ result
28
27
  end
29
28
 
30
- def self.select *args
29
+ def self.select(*args)
31
30
  prompt = TTY::Prompt.new
32
- result = prompt.select *args
33
- return result
31
+ result = prompt.select(*args)
32
+ result
34
33
  end
35
34
 
36
- def self.multi_select *args
35
+ def self.multi_select(*args)
37
36
  prompt = TTY::Prompt.new
38
- result = prompt.multi_select *args
39
- return result
37
+ result = prompt.multi_select(*args)
38
+ result
40
39
  end
41
40
 
42
- def self.enum_select *args
41
+ def self.enum_select(*args)
43
42
  prompt = TTY::Prompt.new
44
- result = prompt.enum_select *args
45
- return result
43
+ result = prompt.enum_select(*args)
44
+ result
46
45
  end
47
46
 
48
- def self.expand *args
47
+ def self.expand(*args)
49
48
  prompt = TTY::Prompt.new
50
- result = prompt.expand *args
51
- return result
49
+ result = prompt.expand(*args)
50
+ result
52
51
  end
53
-
54
- end
52
+ end
@@ -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) != nil rescue false
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
- !!match(/^[[:digit:]]+$/)
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
- !!match(/^[[:alnum:]]+$/)
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
- !!match(/^[[:alpha:]]+$/)
70
+ !match(/^[[:alpha:]]+$/).nil?
70
71
  end
71
72
 
72
73
  def help?
73
- if self.eql? '?' or
74
- self.eql? '-h' or
75
- self.eql? '--help' or
76
- self.eql? 'help'
77
- return true
74
+ if eql?('?') ||
75
+ eql?('-h') ||
76
+ eql?('--help') ||
77
+ eql?('help')
78
+ true
78
79
  else
79
- return false
80
+ false
80
81
  end
81
82
  end
82
-
83
- end
83
+ end
@@ -25,123 +25,147 @@ class ToolsUtil
25
25
  # @param hash
26
26
  # return hash symbolized
27
27
  def self.symbolize_keys(hash)
28
- hash.inject({}){|result, (key, value)|
29
- new_key = case key
30
- when String then key.to_sym
31
- else key
32
- end
33
- new_value = case value
34
- when Hash then symbolize_keys(value)
35
- else value
36
- end
37
- result[new_key] = new_value
38
- result
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? source
49
- begin
50
- !!JSON.parse(source)
51
- rescue JSON::ParserError
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? source
61
- begin
62
- !!YAML.parse(source)
63
- rescue Psych::SyntaxError
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 format='%e %B %Y, %H:%M'
73
- return I18n.l(DateTime.now, :format => format)
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 variable, value
83
- if self.instance_variable_defined? ("@#{variable}")
84
- aux = self.get_variable variable
85
- case aux.class.to_s
86
- when 'String'
87
- self.set_variable variable, value
88
- when 'Hash'
89
- begin
90
- aux.merge! value
91
- self.set_variable variable, aux
92
- rescue
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 variable, value
108
- self.instance_variable_set("@#{variable}", value)
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 variable
116
- return self.instance_variable_get("@#{variable}")
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
- return self.instance_variables
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 value
131
- case value.class.to_s
132
- when 'String'
133
- return "\t#{value.yellow}"
134
- when 'Hash','Array'
135
- old_stdout = $stdout
136
- captured_stdio = StringIO.new('', 'w')
137
- $stdout = captured_stdio
138
- ap value, {:plain => true}
139
- $stdout = old_stdout
140
- value = captured_stdio.string
141
- return value
142
- else
143
- return value
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
- end
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
@@ -1,4 +1,4 @@
1
- require "tools/version"
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
- #ToolsUtil.instance
79
+ # ToolsUtil.instance
82
80
  end
83
-
84
-
85
-