Module: Migr8::Util::Expander

Defined in:
lib/migr8.rb

Defined Under Namespace

Classes: UnknownVariableError

Class Method Summary collapse

Class Method Details

.expand_str(str, dict) ⇒ Object



2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
# File 'lib/migr8.rb', line 2197

def expand_str(str, dict)
  raise unless dict.is_a?(Hash)
  if str =~ /\A\$\{(.*?)\}\z/
    var = $1
    if var.empty?
      return ''
    elsif dict.key?(var)
      return dict[var]
    else
      raise UnknownVariableError.new("${#{var}}: no such variable.")
    end
  else
    return str.gsub(/\$\{(.*?)\}/) {
      var = $1
      if var.empty?
        ''
      elsif dict.key?(var)
        dict[var].to_s
      else
        raise UnknownVariableError.new("${#{var}}: no such variable.")
      end
    }
  end
end

.expand_value(value, dict) ⇒ Object



2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
# File 'lib/migr8.rb', line 2174

def expand_value(value, dict)
  case value
  when String
    return expand_str(value, dict)
  when Array
    arr = value
    i = 0
    while i < arr.length
      arr[i] = expand_value(arr[i], dict)
      i += 1
    end
    return arr
  when Hash
    hash = value
    hash.keys.each do |k|
      hash[k] = expand_value(hash[k], dict)
    end
    return hash
  else
    return value
  end
end

.expand_vars(vars) ⇒ Object



2164
2165
2166
2167
2168
2169
2170
2171
2172
# File 'lib/migr8.rb', line 2164

def expand_vars(vars)
  dict = {}
  vars.each do |d|
    d.each do |k, v|
      dict[k] = expand_value(v, dict)
    end
  end if vars
  return dict
end