Class: Pathology::Template

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

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Template

Returns a new instance of Template.



4
5
6
# File 'lib/pathology/template.rb', line 4

def initialize(pattern)
  @pattern = pattern
end

Instance Method Details

#interpolate(values) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/pathology/template.rb', line 8

def interpolate(values)
  sorted_values = values.sort_by{|token, replacement| token.length}.reverse

  sorted_values.reduce(@pattern) do |interpolated, value|
    token, replacement = *value
    interpolated.gsub(/:#{token}/, replacement.to_s)
  end
end