Class: ToolsUtil
Class Method Summary collapse
-
.get_date(format = '%e %B %Y, %H:%M') ⇒ String
Return a formated DateTime.
-
.get_plain_text(value) ⇒ Object
Return a plain text for content of String or Hash or Array.
- .get_plain_text_if_hash_or_array(value) ⇒ Object
-
.get_variable(variable) ⇒ Object
Get a existent class variable.
-
.get_variables ⇒ Object
Get all existent class variablea.
- .map_value(thing) ⇒ Object
- .set_array_variable(variable, value, aux) ⇒ Object
- .set_hash_variable(variable, value, aux) ⇒ Object
- .set_string_variable(variable, value) ⇒ Object
-
.set_variable(variable, value) ⇒ Object
Set a new class variable.
-
.set_variable_ext(variable, value) ⇒ Object
Modify the class variable..
-
.symbolize_keys(hash) ⇒ Object
Synbolize all keys in hash.
-
.valid_json?(source) ⇒ Boolean
Test a valid json string.
-
.valid_yaml?(source) ⇒ Boolean
Test a valid yaml string.
Class Method Details
.get_date(format = '%e %B %Y, %H:%M') ⇒ String
Return a formated DateTime.
79 80 81 |
# File 'lib/lib/utils.rb', line 79 def self.get_date(format = '%e %B %Y, %H:%M') I18n.l(DateTime.now, format: format) end |
.get_plain_text(value) ⇒ Object
Return a plain text for content of String or Hash or Array.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/lib/utils.rb', line 147 def self.get_plain_text(value) case value when String then "\t#{value.yellow}" when Hash, Array # old_stdout = $stdout # captured_stdio = StringIO.new('', 'w') # $stdout = captured_stdio # ap value, plain: true # $stdout = old_stdout # captured_stdio.string get_plain_text_if_hash_or_array value else value end end |
.get_plain_text_if_hash_or_array(value) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/lib/utils.rb', line 163 def self.get_plain_text_if_hash_or_array(value) old_stdout = $stdout captured_stdio = StringIO.new('', 'w') $stdout = captured_stdio ap value, plain: true $stdout = old_stdout captured_stdio.string end |
.get_variable(variable) ⇒ Object
Get a existent class variable.
132 133 134 |
# File 'lib/lib/utils.rb', line 132 def self.get_variable(variable) instance_variable_get("@#{variable}") end |
.get_variables ⇒ Object
Get all existent class variablea.
139 140 141 |
# File 'lib/lib/utils.rb', line 139 def self.get_variables instance_variables end |
.map_value(thing) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lib/utils.rb', line 44 def self.map_value(thing) case thing when Hash symbolize_keys(thing) when Array thing.map { |v| map_value(v) } else thing end end |
.set_array_variable(variable, value, aux) ⇒ Object
114 115 116 117 |
# File 'lib/lib/utils.rb', line 114 def self.set_array_variable(variable, value, aux) aux.insert(-1, value) set_variable variable, aux end |
.set_hash_variable(variable, value, aux) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/lib/utils.rb', line 106 def self.set_hash_variable(variable, value, aux) aux.merge! value set_variable variable, aux rescue StandardError ToolsDisplay.show "\tToolsDisplay error [set_variable_ext].".light_red ToolsDisplay.show "\tAttempt insert #{variable.class} into Hash variable....".light_red end |
.set_string_variable(variable, value) ⇒ Object
102 103 104 |
# File 'lib/lib/utils.rb', line 102 def self.set_string_variable(variable, value) set_variable variable, value end |
.set_variable(variable, value) ⇒ Object
Set a new class variable.
124 125 126 |
# File 'lib/lib/utils.rb', line 124 def self.set_variable(variable, value) instance_variable_set("@#{variable}", value) end |
.set_variable_ext(variable, value) ⇒ Object
Modify the class variable.. String => change value, Hash => merge, Array => insert
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lib/utils.rb', line 88 def self.set_variable_ext(variable, value) return unless instance_variable_defined? "@#{variable}" aux = get_variable variable case aux.class.to_s when 'String' set_string_variable variable, value when 'Hash' set_hash_variable variable, value, aux when 'Array' set_array_variable variable, value, aux end end |
.symbolize_keys(hash) ⇒ Object
Synbolize all keys in hash.
return hash symbolized
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lib/utils.rb', line 27 def self.symbolize_keys(hash) {}.tap do |h| hash.each { |key, value| h[key.to_sym] = map_value(value) } end # hash.each_with_object({}) do |(key, value), result| # 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 # end end |
.valid_json?(source) ⇒ Boolean
Test a valid json string.
59 60 61 62 63 |
# File 'lib/lib/utils.rb', line 59 def self.valid_json?(source) JSON.parse(source) && true rescue JSON::ParserError false end |
.valid_yaml?(source) ⇒ Boolean
Test a valid yaml string.
69 70 71 72 73 |
# File 'lib/lib/utils.rb', line 69 def self.valid_yaml?(source) YAML.parse(source) && true rescue Psych::SyntaxError false end |