Class: VariableInterpolator

Inherits:
Object
  • Object
show all
Defined in:
lib/variable_interpolator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}) ⇒ VariableInterpolator

Returns a new instance of VariableInterpolator.



5
6
7
# File 'lib/variable_interpolator.rb', line 5

def initialize(variables = {})
  @variables = variables.map { |key, value| [key.to_sym, value] }.to_h
end

Instance Attribute Details

#variablesObject

Returns the value of attribute variables.



3
4
5
# File 'lib/variable_interpolator.rb', line 3

def variables
  @variables
end

Instance Method Details

#interpolate(input) ⇒ Object



9
10
11
12
13
14
# File 'lib/variable_interpolator.rb', line 9

def interpolate(input)
  input.gsub(/\${[a-zA-Z0-9\-_]*}/) do |m|
    var = m.scan(/[a-zA-Z0-9\-_]+/).first.to_sym
    variables[var]
  end
end