Class: TranslationDiff::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/translation_diff/fragment.rb

Overview

A run of the source that is either markup, handed back as found, or prose, handed back through its segments.

Constant Summary collapse

EMPTY =
[].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, segments) ⇒ Fragment

Returns a new instance of Fragment.



22
23
24
25
# File 'lib/translation_diff/fragment.rb', line 22

def initialize(source, segments)
  @source = source
  @segments = segments
end

Instance Attribute Details

#source ⇒ Object (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/translation_diff/fragment.rb', line 5

def source
  @source
end

Class Method Details

.markup(source) ⇒ Object

Markup has nothing a provider should see, so it carries no segments and renders the bytes it was cut from.



8
# File 'lib/translation_diff/fragment.rb', line 8

def self.markup(source) = new(source, nil)

.prose(source, segmenter:, language: nil) ⇒ Object

Prose is cut where the segmenter says sentences begin, so every segment keeps the whitespace it was found in.



11
12
13
# File 'lib/translation_diff/fragment.rb', line 11

def self.prose(source, segmenter:, language: nil)
  new(source, cut(source, segmenter.split_offsets(source, language: language)))
end

Instance Method Details

#markup? ⇒ Boolean

Returns:

  • (Boolean)


27
# File 'lib/translation_diff/fragment.rb', line 27

def markup? = @segments.nil?

#render ⇒ Object

The slice is copied on the way out, as Segment copies its own: rendering must not hand a caller the passage.



32
# File 'lib/translation_diff/fragment.rb', line 32

def render = markup? ? source.dup : @segments.map(&:render).join

#segments ⇒ Object



29
# File 'lib/translation_diff/fragment.rb', line 29

def segments = @segments || EMPTY