Class: Pechkin::Substitute

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

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(substitutions) ⇒ Substitute

Returns a new instance of Substitute.

Parameters:

  • substitutions (Hash)

    hash of possible substitutions for replacement



10
11
12
# File 'lib/pechkin/substitute.rb', line 10

def initialize(substitutions)
  @substitutions = substitutions
end

Instance Method Details

#process(string) ⇒ Object



14
15
16
17
18
19
20
21
22
# 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