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
# 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
  tools_logfile = Tools.home+'/.tools/tools.log'
  ToolsLog.create_log_file 'tools', tools_logfile
end

Class Method Details

.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:



44
45
46
# File 'lib/lib/utils.rb', line 44

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lib/utils.rb', line 107

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



87
88
89
# File 'lib/lib/utils.rb', line 87

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

.instance_variable?(variable) ⇒ Boolean

Check a existent class variable.

Parameters:

  • variable —

    variable name to retrive

Returns:

  • (Boolean) —

    boolean



95
96
97
98
99
# File 'lib/lib/utils.rb', line 95

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

.purge_files(path, select, time) ⇒ Object

sem minitest



129
130
131
132
133
134
135
# File 'lib/lib/utils.rb', line 129

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:

  • []



79
80
81
# File 'lib/lib/utils.rb', line 79

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:

  • []



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lib/utils.rb', line 54

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

.valid_json?(source) ⇒ Boolean

Test a valid json string.

Parameters:

  • Json —

    string to be tested

Returns:

  • (Boolean) —

    boolean



20
21
22
23
24
25
26
# File 'lib/lib/utils.rb', line 20

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:

  • Yaml —

    string to be tested

Returns:

  • (Boolean) —

    boolean



32
33
34
35
36
37
38
# File 'lib/lib/utils.rb', line 32

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