Class: Realize::Format::StringReplace

Inherits:
Object
  • Object
show all
Defined in:
lib/realize/format/string_replace.rb

Overview

This transformer takes in a value and replaces all occurrences of the given original pattern with the replacement pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original:, replacement:) ⇒ StringReplace

Returns a new instance of StringReplace.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
# File 'lib/realize/format/string_replace.rb', line 19

def initialize(original:, replacement:)
  raise ArgumentError, 'original is required' if original.to_s.empty?

  @original    = original
  @replacement = replacement.to_s

  freeze
end

Instance Attribute Details

#originalObject (readonly)

Returns the value of attribute original.



17
18
19
# File 'lib/realize/format/string_replace.rb', line 17

def original
  @original
end

#replacementObject (readonly)

Returns the value of attribute replacement.



17
18
19
# File 'lib/realize/format/string_replace.rb', line 17

def replacement
  @replacement
end

Instance Method Details

#transform(_resolver, value, _time, _record) ⇒ Object



28
29
30
# File 'lib/realize/format/string_replace.rb', line 28

def transform(_resolver, value, _time, _record)
  value.to_s.gsub(original, replacement)
end