Class: ToolsUtil

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/lib/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ToolsUtil

Returns a new instance of ToolsUtil.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lib/utils.rb', line 5

def initialize(options = {})
  I18n.load_path = Dir[Tools.files + '/pt-BR.yml']
  I18n.locale    = 'pt-BR'.to_sym
  ToolsDisplay.instance
  ToolsFiles.instance
  ToolsConfig.instance
  ToolsLog.instance
  ToolsPrompt.instance
  unless File.exists? Tools.home+'/.tools'
    FileUtils.mkdir_p(Tools.home+'/.tools')
  end
  tools_logfile = Tools.home+'/.tools/tools.log'
  ToolsLog.create_log_file 'tools', tools_logfile
end

Class Method Details

.ask_prompt(prompt = '', hidden = true) ⇒ Object

Capture string from a prompt.

Parameters:

  • prompt (defaults to: '')
  • hidden (defaults to: true) —

    option

Returns:

  • String



45
46
47
48
49
50
51
# File 'lib/lib/utils.rb', line 45

def self.ask_prompt(prompt = '', hidden = true)
  if hidden
    a = ask("#{prompt}") { |q| q.echo = "*"; } #q.case = :downcase }
  else
    a = ask("#{prompt}") #{ |q| q.case = :downcase }
  end
end

.get_date(format = '%e %B %Y, %H:%M') ⇒ String

Return a formated DateTime.

Parameters:

  • format (defaults to: '%e %B %Y, %H:%M') —

    with a DateTime format default are: 12 Outubro 2018, 08:29

Returns:



81
82
83
# File 'lib/lib/utils.rb', line 81

def self.get_date format='%e %B %Y, %H:%M'
  return I18n.l(DateTime.now, :format => format)
end

.get_plain_text(value) ⇒ Object

Return a plain text for content of String or Hash or Array.

Parameters:

  • value —

    Content of variable to translate to Plaint text

Returns:

  • plain text content



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lib/utils.rb', line 149

def self.get_plain_text value
 case value.class.to_s
    when 'String'
      return  "\t#{value.yellow}"
    when 'Hash','Array'
      old_stdout = $stdout
      captured_stdio = StringIO.new('', 'w')
      $stdout = captured_stdio
      ap value, {:plain => true}
      $stdout = old_stdout
      value = captured_stdio.string
      return value
    else
      return value
    end
end

.get_variable(variable) ⇒ Object

Get a existent class variable.

Parameters:

  • variable —

    variable name to retrive

Returns:

  • variable value



124
125
126
# File 'lib/lib/utils.rb', line 124

def self.get_variable variable
  return self.instance_variable_get("@#{variable}")
end

.get_variables ⇒ Object

Get all existent class variablea.

Returns:

  • variables



141
142
143
# File 'lib/lib/utils.rb', line 141

def self.get_variables
  return self.instance_variables
end

.instance_variable?(variable) ⇒ Boolean

Check a existent class variable.

Parameters:

  • variable —

    variable name to retrive

Returns:

  • (Boolean) —

    boolean



132
133
134
135
136
# File 'lib/lib/utils.rb', line 132

def self.instance_variable? variable
  # ap self.instance_variables.include? variable.to_sym
  # exit
  return self.instance_variable_get(":@#{variable}")
end

.purge_files(path, select, time) ⇒ Object

sem minitest



171
172
173
174
175
176
177
# File 'lib/lib/utils.rb', line 171

def self.purge_files path, select, time #Cmdapi.configuration.home+'/.cmdapi/backup', '*',   14*24*60*60
  to_clean = Dir.glob(File.join(path, select)).select { |a|
    Time.now - File.ctime(a) > time }
  to_clean.each do |file_to_delete|
    File.delete(file_to_delete)
  end
end

.set_variable(variable, value) ⇒ Object

Set a new class variable.

Parameters:

  • variable —

    variable name to set

  • value —

    value for variable

Returns:

  • []



116
117
118
# File 'lib/lib/utils.rb', line 116

def self.set_variable variable, value
  self.instance_variable_set("@#{variable}", value)
end

.set_variable_ext(variable, value) ⇒ Object

Modify the class variable.. String => change value, Hash => merge, Array => insert

Parameters:

  • variable —

    name variable

  • value —

    value veriable

Returns:

  • []



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lib/utils.rb', line 91

def self.set_variable_ext variable, value
  if self.instance_variable_defined? ("@#{variable}")
    aux = self.get_variable variable
    case aux.class.to_s
    when 'String'
       self.set_variable variable, value
    when 'Hash'
       begin
        aux.merge! value
        self.set_variable variable, aux
      rescue
        ToolsDisplay.show "\tToolsDisplay error [set_variable_ext]. Attempt insert #{variable.class} into Hash variable....".light_red
      end
    when 'Array'
      aux.insert(-1,value)
      self.set_variable variable, aux
    end
  end
end

.symbolize_keys(hash) ⇒ Object

Synbolize all keys in hash.

return hash symbolized

Parameters:

  • hash


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lib/utils.rb', line 24

def self.symbolize_keys(hash)
  hash.inject({}){|result, (key, value)|
    new_key = case key
              when String then key.to_sym
              else key
              end
    new_value = case value
                when Hash then symbolize_keys(value)
                else value
                end
    result[new_key] = new_value
    result
  }
end

.valid_json?(source) ⇒ Boolean

Test a valid json string.

Parameters:

  • source —

    Json string to be tested

Returns:

  • (Boolean) —

    boolean



57
58
59
60
61
62
63
# File 'lib/lib/utils.rb', line 57

def self.valid_json? source
  begin
    !!JSON.parse(source)
  rescue JSON::ParserError
    false
  end
end

.valid_yaml?(source) ⇒ Boolean

Test a valid yaml string.

Parameters:

  • source —

    Yaml string to be tested

Returns:

  • (Boolean) —

    boolean



69
70
71
72
73
74
75
# File 'lib/lib/utils.rb', line 69

def self.valid_yaml? source
  begin
    !!YAML.parse(source)
  rescue Psych::SyntaxError
    false
  end
end