Class: Olyx::Guardrails::PolicyComponents::UnmatchedSegmentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/olyx/guardrails/policy/unmatched_segment_builder.rb

Overview

Accumulates transformed gaps and fixed policy replacements in source order.

Instance Method Summary collapse

Constructor Details

#initialize(source, transform) ⇒ UnmatchedSegmentBuilder

Returns a new instance of UnmatchedSegmentBuilder.



8
9
10
11
12
13
# File 'lib/olyx/guardrails/policy/unmatched_segment_builder.rb', line 8

def initialize(source, transform)
  @source = source
  @transform = transform
  @cursor = 0
  @parts = []
end

Instance Method Details

#append(span) ⇒ Object



15
16
17
18
19
# File 'lib/olyx/guardrails/policy/unmatched_segment_builder.rb', line 15

def append(span)
  @parts << @transform.call(@source[@cursor...span[:start]])
  @parts << span[:replacement]
  @cursor = span[:end]
end

#finishObject



21
22
23
24
# File 'lib/olyx/guardrails/policy/unmatched_segment_builder.rb', line 21

def finish
  @parts << @transform.call(@source[@cursor..])
  @parts.join
end