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:$mysql_admin_pass@mysql: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, #reset_tried, resolvers, #set_tried, #tried?, #try_resolve

Constructor Details

This class inherits a constructor from Opto::Resolver

Instance Method Details

#afterObject



29
30
31
# File 'lib/opto/resolvers/interpolate.rb', line 29

def after
  reset_tried
end

#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('${}', '')
    if option.group.nil? || option.group.option(var).nil?
      raise RuntimeError, "Variable #{var} not declared"
    end
    if option.value_of(var).nil?
      raise RuntimeError, "No value for #{var}, note that the order is meaningful"
    end
    option.value_of(var)
  end
end