class Pechkin::Substitute

Replaces ${:varname:} patterns inside strings. All posible substitutions are provided through constructor.

Complex templating and text fromatting (like float numbers formatting) is not a goal. We do not aim to implement new templating engine here. Just simple stuff.

Public Class Methods

new(substitutions) click to toggle source

@param substitutions [Hash] hash of possible substitutions for replacement

# File lib/pechkin/substitute.rb, line 10
def initialize(substitutions)
  @substitutions = substitutions
end

Public Instance Methods

process(string) click to toggle source
# File lib/pechkin/substitute.rb, line 14
def process(string)
  string.gsub(/\$\{([A-Za-z0-9_]+)\}/) do |m|
    key = m[2..-2]

    value = @substitutions[key] || @substitutions[key.to_sym]

    (value || m).to_s
  end
end