Class: Utils
- Inherits:
-
Object
- Object
- Utils
- Defined in:
- lib/helper/auto_util.rb
Class Method Summary collapse
- .bind_with_dyn_json_vars(json, bind_json) ⇒ Object
-
.bind_with_dyn_vars(str) ⇒ Object
bind string with $dyn_vars context.
- .check_dynamic_value(value) ⇒ Object
-
.eval_with_dyn_vars(operation) ⇒ Object
evaluate operation/statement with $dyn_vars context.
- .print_variable(variable) ⇒ Object
- .replace_day(str) ⇒ Object
- .replace_pipe(str_pipe) ⇒ Object
- .replace_year(str) ⇒ Object
- .resolve_params(url) ⇒ Object
-
.set_var(var_name, var_value) ⇒ Object
set value to a variable.
- .store_string_as_variable(string, variable) ⇒ Object
- .var_collect(string_var) ⇒ Object
Instance Method Summary collapse
Class Method Details
.bind_with_dyn_json_vars(json, bind_json) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/helper/auto_util.rb', line 63 def self.bind_with_dyn_json_vars(json, bind_json) if json.kind_of? Hash json.each_pair do |k, v| if v.kind_of? String bind_json[bind_with_dyn_vars(k)] = bind_with_dyn_json_vars(v, bind_json) elsif v.kind_of? Hash temp = Hash.new v.each_pair do |k1, v1| temp[bind_with_dyn_vars(k1)] = bind_with_dyn_json_vars(v1, temp) end bind_json[bind_with_dyn_vars(k)] = temp elsif v.kind_of? Array temp1 = Array.new v.each { |item| temp2 = Hash.new bind_with_dyn_json_vars(item, temp2) temp1 << temp2 } bind_json[bind_with_dyn_vars(k)] = temp1 end end elsif json.kind_of? Array temp1 = Array.new json.each { |item| temp2 = Hash.new bind_with_dyn_json_vars(item, temp2) temp1 << temp2 } return temp1 else return bind_with_dyn_vars(json) end end |
.bind_with_dyn_vars(str) ⇒ Object
bind string with $dyn_vars context
45 46 47 48 49 50 51 52 |
# File 'lib/helper/auto_util.rb', line 45 def self.bind_with_dyn_vars(str) if $dyn_vars == nil $dyn_vars = binding end strEval = '"' + str + '"' return eval strEval, $dyn_vars end |
.check_dynamic_value(value) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/helper/auto_util.rb', line 17 def self.check_dynamic_value value if !value.is_a? Fixnum if value.include? "params=" resolve_params value else bind_with_dyn_vars value end else value end end |
.eval_with_dyn_vars(operation) ⇒ Object
evaluate operation/statement with $dyn_vars context
55 56 57 58 59 60 61 |
# File 'lib/helper/auto_util.rb', line 55 def self.eval_with_dyn_vars(operation) if $dyn_vars == nil $dyn_vars = binding end eval operation, $dyn_vars end |
.print_variable(variable) ⇒ Object
160 161 162 |
# File 'lib/helper/auto_util.rb', line 160 def self.print_variable(variable) puts "VALUE OF #{variable}: #{Utils.eval_with_dyn_vars(variable)}" end |
.replace_day(str) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/helper/auto_util.rb', line 134 def self.replace_day str t = Time.now() _day = t.day if _day < 10 return str.gsub("{day}", "0#{_day.to_s}") else return str.gsub("{day}", "#{_day.to_s}") end return str end |
.replace_pipe(str_pipe) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/helper/auto_util.rb', line 145 def self.replace_pipe str_pipe put_log ">>>> #{str_pipe}" if str_pipe =~ /^.*{pipe}.*$/ return str_pipe.gsub("{pipe}", "|") end return str_pipe end |
.replace_year(str) ⇒ Object
118 119 120 121 |
# File 'lib/helper/auto_util.rb', line 118 def self.replace_year str t = Time.now() return str.gsub("{year}", t.year.to_s) end |
.resolve_params(url) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/helper/auto_util.rb', line 30 def self.resolve_params url condition = url.match(/params='([^']*)'/)[0] if condition params_name = url.match(/params='([^']*)'/)[1] params_value = $PARAMS["#{params_name.downcase}"].to_s.strip if params_value.present? url = url.gsub(condition, params_value) else raise "ERROR: no data found with given params #{params_name}" end end url end |
.set_var(var_name, var_value) ⇒ Object
set value to a variable
8 9 10 11 12 13 14 15 |
# File 'lib/helper/auto_util.rb', line 8 def self.set_var(var_name, var_value) if $dyn_vars == nil $dyn_vars = binding end strEval = var_name + "=" + var_value eval strEval, $dyn_vars end |
.store_string_as_variable(string, variable) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/helper/auto_util.rb', line 153 def self.store_string_as_variable(string,variable) # $context_value = Utils.bind_with_dyn_vars(string) # Utils.set_var(variable, '$context_value') require 'erb' ERB.new(string, 0, "", "#{variable}").result(binding) end |
.var_collect(string_var) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/helper/auto_util.rb', line 98 def self.var_collect string_var if string_var =~ /^.*{year}.*$/ string_var = replace_year(string_var) end if string_var =~ /^.*{month}.*$/ string_var = replace_month(string_var) end if string_var =~ /^.*{day}.*$/ string_var = replace_day(string_var) end if string_var =~ /^.*{store_value}.*$/ string_var = $context_value end return string_var end |
Instance Method Details
#replace_month(str) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/helper/auto_util.rb', line 123 def replace_month str t = Time.now() _month = t.month if _month < 10 return str.gsub("{month}", "0#{_month.to_s}") else return str.gsub("{month}", "#{_month.to_s}") end return str end |