class Util

Public Class Methods

bind_json_with_dyn_vars(data_json) click to toggle source

bind json with $dyn_vars context

# File lib/anhpham/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) click to toggle source

bind string with $dyn_vars context

# File lib/anhpham/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) click to toggle source
# File lib/anhpham/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) click to toggle source
# File lib/anhpham/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) click to toggle source
# File lib/anhpham/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) click to toggle source
# File lib/anhpham/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) click to toggle source

set value to a variable

# File lib/anhpham/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