Class: Util

Inherits:
Object
  • Object
show all
Defined in:
lib/Ifd_Automation/methods/util.rb

Class Method Summary collapse

Class Method Details

.bind_json_with_dyn_vars(data_json) ⇒ Object

bind json with $dyn_vars context



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/Ifd_Automation/methods/util.rb', line 28

def self.bind_json_with_dyn_vars(data_json)
  if data_json.kind_of? Hash
    data_json.each_pair { |k,v|
      data_json[k] = Util.bind_json_with_dyn_vars(v)
    }
    for i in 0..data_json.keys.size - 1
      if data_json.keys[i] != Util.bind_with_dyn_vars(data_json.keys[i])
        k = Util.bind_with_dyn_vars(data_json.keys[i])
        v = data_json[data_json.keys[i]]

        data_json.delete(data_json.keys[i])
        data_json[k] = v
      end
    end
    return data_json
  elsif data_json.kind_of? Array
    for i in 0..data_json.size - 1
      data_json[i] = Util.bind_json_with_dyn_vars(data_json[i])
    end
    return data_json
  elsif data_json.kind_of? String
    return Util.bind_with_dyn_vars(data_json)
  else
    raise "*** ERROR: unexpected error at cirrus_utils. Util.bind_json_with_dyn_vars function."
  end
end

.bind_with_dyn_vars(str) ⇒ Object

bind string with $dyn_vars context



18
19
20
21
22
23
24
25
# File 'lib/Ifd_Automation/methods/util.rb', line 18

def self.bind_with_dyn_vars(str)
  if $dyn_vars == nil
    $dyn_vars = binding
  end

  strEval = '"' + str +'"'
  return eval strEval, $dyn_vars
end

.convert_to_symbol_json(json) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/Ifd_Automation/methods/util.rb', line 106

def self.convert_to_symbol_json(json)
  begin
    json = JSON.parse(json)
  rescue
  end

  if json.kind_of? Hash
    json_sym = Hash.new()
    json.each {|k,v|
      json_sym[k.downcase.to_sym] = convert_to_symbol_json(v)
    }
    return json_sym
  elsif json.kind_of? Array
    array_sym = Array.new()
    json.each {|temp|
      array_sym << convert_to_symbol_json(temp)
    }
    return array_sym
  else
    return json
  end
end

.generate_condition_statement(condition_json, wildcard_type = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/Ifd_Automation/methods/util.rb', line 129

def self.generate_condition_statement(condition_json, wildcard_type = nil)
  v_condition = ''

  condition_json.each_pair { |col, vals|
    #puts "DatabaseMethods.generate_condition_statement.col: #{col}"
    #puts "DatabaseMethods.generate_condition_statement.vals: #{vals}"
    condition = ''
    if vals.nil?
      condition = "#{col} is null"
      # elsif self.metadata[:datetime_cols].include? col.downcase.to_sym
      #   condition = "#{col} >= to_date(#{DB_Utils.sql_value_format(vals)}, 'yyyy-mm-dd') AND #{col} < to_date(#{DB_Utils.sql_value_format(vals)}, 'yyyy-mm-dd') + 1"
    else
      vals = vals.to_s.split(',') unless vals.kind_of? Array

      vals = vals.map { |val|
        if wildcard_type.nil?
          DB_Utils.sql_value_format(val)
        else
          case wildcard_type.to_s.downcase.to_sym
            when :prefix
              DB_Utils.sql_value_format_prefix_wildcard(val)
            when :suffix
              DB_Utils.sql_value_format_suffix_wildcard(val)
            when :wildcard
              DB_Utils.sql_value_format_wildcard(val)
            else
              DB_Utils.sql_value_format(val)
          end
        end
      }
      vals.each { |val|
        temp_condition = val == 'null' ? "#{col} is #{val}" : "#{col} like #{val}"
        condition += condition.empty? ? temp_condition : " OR #{temp_condition}"
      }
    end
    v_condition += v_condition.empty? ? "(#{condition})" : " AND (#{condition})"
  }
  v_condition
end

.read_table_data_from_steps_with_header(table_data) ⇒ Object



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

def self.read_table_data_from_steps_with_header(table_data)
  # The table include header so the loop starting from 1 instead of 0
  json_array = Array.new
  for i in 1..table_data.size - 1
    json_temp = Hash.new
    for j in 0..table_data[0].size - 1
      json_temp[table_data[0][j]] = table_data[i][j]
    end
    json_array << json_temp
  end

  # get dyn_vars if any
  json_array = Util.bind_json_with_dyn_vars(json_array)

  # replace [[TODAY]] with current time
  json_array = Util.replace_json_with_date_holders(json_array)
  json_array
end

.replace_json_with_date_holders(json_string) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/Ifd_Automation/methods/util.rb', line 74

def self.replace_json_with_date_holders(json_string)
  # replace [[TODAY]] = current date
  # replace [[NULL]] = nil

  if json_string.kind_of? Hash
    #puts "Hash: #{json_string}"
    json_string.each_pair {|k, v|
      json_string[k] = replace_json_with_date_holders(v)
    }
    return json_string
  elsif json_string.kind_of? Array
    #puts "Array: #{json_string}"
    for i in 0..json_string.size - 1
      json_string[i] = replace_json_with_date_holders(json_string[i])
    end
    return json_string
  elsif json_string.kind_of? String
    #puts "String: #{json_string}"
    if json_string.include? "[[TODAY]]"
      json_string = eval json_string.gsub("[[TODAY]]", "Date.today")
      json_string = json_string.strftime('%Y-%m-%d')
    end
    if json_string == "[[NULL]]"
      json_string = nil
    end

    return json_string
  else
    raise "*** ERROR: unexpected error at Environment.replace_json_with_date_holder function."
  end
end

.set_var(var_name, var_value) ⇒ Object

set value to a variable



9
10
11
12
13
14
15
# File 'lib/Ifd_Automation/methods/util.rb', line 9

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