Sha256: 50af106ac686b3eeb62ff87056a08552ba2f53270753e205e2707b5bd7f5b9da

Contents?: true

Size: 785 Bytes

Versions: 3

Compression:

Stored size: 785 Bytes

Contents

# frozen_string_literal: true

module TableSaw
  class VariableInterpolation
    def self.call(variables)
      new(variables).call
    end

    attr_reader :input

    def initialize(input)
      @input = input
    end

    def call
      input.transform_values do |value|
        next value unless value.is_a?(String)

        interpolate_variables(value, input)
      end
    end

    private

    def interpolate_variables(string, variables)
      string.gsub(/%{(\w+)}/) do |match|
        variable_name = ::Regexp.last_match(1)
        if variables.key?(variable_name)
          nested_value = interpolate_variables(variables[variable_name], variables)
          interpolate_variables(nested_value, variables)
        else
          match
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_saw-3.2.0 lib/table_saw/variable_interpolation.rb
table_saw-3.1.0 lib/table_saw/variable_interpolation.rb
table_saw-3.0.0 lib/table_saw/variable_interpolation.rb