class ERBResolve

Public Class Methods

new(vars) click to toggle source

Resolve variables for the given data type @param vars [Hash/OpenStruct] hash or OpenStruct of ERB variables to use

# File lib/nub/core.rb, line 55
def initialize(vars)
  raise ArgumentError.new("Variables are required") if not vars

  @vars = vars.is_a?(OpenStruct) ? vars.to_h : vars
  @context = ERBContext.new(@vars).get_binding
end

Public Instance Methods

resolve(data) click to toggle source

Resolve variables for the given data type @data [string/array/hash] data to replace vars @returns mutated data structure

# File lib/nub/core.rb, line 65
def resolve(data)

  # Recurse
  if data.is_a?(Array)
    data = data.map{|x| resolve(x)}
  elsif data.is_a?(Hash)
    data.each{|k,v| data[k] = resolve(v)}
  end

  # Base case
  if data.is_a?(String)
    data = ERB.new(data).result(@context)
  end

  return data
end
resolve!(data) click to toggle source

Resolve variables for the given data type, changes are done inplace @data [string/array/hash] data to replace vars

# File lib/nub/core.rb, line 84
def resolve!(data)

  # Recurse
  if data.is_a?(Array)
    for i in 0...data.size
      resolve!(data[i])
    end
  elsif data.is_a?(Hash)
    data.each{|k,v| data[k] = resolve!(v)}
  end

  # Base case
  if data.is_a?(String)
    data.gsub!(data, ERB.new(data).result(@context))
  end

  return data
end