Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/yolo_backup/core_ext/string.rb

Constant Summary collapse

INTERPOLATION_PATTERN =
Regexp.union(
  /%%/,
  /%\{(\w+)\}/
)

Instance Method Summary collapse

Instance Method Details

#interpolate(values) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yolo_backup/core_ext/string.rb', line 6

def interpolate(values)
  self.gsub(INTERPOLATION_PATTERN) do |match|
    if match == '%%'
      '%'
    else
      key = ($1 || $2).to_sym
      unless values.key?(key)
        raise "Mission interpolation argument: #{key}"
      end
      value = values[key]
      value = value.call(values) if value.respond_to?(:call)
      $3 ? sprintf("%#{$3}", value) : value
    end
  end
end