Class: Opto::Resolvers::Interpolate

Inherits:
Opto::Resolver show all
Defined in:
lib/opto/resolvers/interpolate.rb

Overview

Interpolates values from other options into a template string.

Example: from:

interpolate: mysql://admin:[email protected]:1234/$mysql_db_name

Instance Attribute Summary

Attributes inherited from Opto::Resolver

#hint, #option

Instance Method Summary collapse

Methods inherited from Opto::Resolver

for, inherited, #initialize, origin, #origin, resolvers

Constructor Details

This class inherits a constructor from Opto::Resolver

Instance Method Details

#resolveObject

Raises:

  • (TypeError)

15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/opto/resolvers/interpolate.rb', line 15

def resolve
  raise TypeError, "String expected" unless hint.kind_of?(String)
  hint.gsub(/(?<!\$)\$(?!\$)\{?[\w\.]+\}?/) do |v|
    var = v.tr('${}', '')

    raise RuntimeError, "Variable #{var} not declared" if option.group.nil?
    opt = option.group.option(var)
    raise RuntimeError, "Variable #{var} not declared" if opt.nil?
    value = opt.value
    raise RuntimeError, "No value for #{var}, note that the order is meaningful" if value.nil?
    value
  end
end