Class: DatadogExporter::Monitors::Utilities::TemplateManager::StringToPlaceholder

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog_exporter/monitors/utilities/template_manager.rb

Overview

This class replaces the matching text with a placeholder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_string, placeholders) ⇒ StringToPlaceholder

Returns a new instance of StringToPlaceholder.



12
13
14
15
# File 'lib/datadog_exporter/monitors/utilities/template_manager.rb', line 12

def initialize(original_string, placeholders)
  @original_string = original_string
  @placeholders = placeholders
end

Instance Attribute Details

#original_stringObject (readonly)

Returns the value of attribute original_string.



10
11
12
# File 'lib/datadog_exporter/monitors/utilities/template_manager.rb', line 10

def original_string
  @original_string
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



10
11
12
# File 'lib/datadog_exporter/monitors/utilities/template_manager.rb', line 10

def placeholders
  @placeholders
end

Instance Method Details

#replaceObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/datadog_exporter/monitors/utilities/template_manager.rb', line 17

def replace
  replaced_string = original_string.dup

  placeholders.each do |placeholder_name, matching_text|
    raise "Unknown placeholder value for #{placeholder_name}" if matching_text == ""

    placeholder = "#{placeholder_name}_placeholder"
    original_string.include?(matching_text) &&
      replaced_string.gsub!(matching_text, placeholder)
  end

  replaced_string
end