Class: TranslationDiff::Segment

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

Overview

A sentence with the whitespace it was found in: its core is the text a provider sees, its render is markup again.

Constant Summary collapse

BLANK =

A core is compared decoded, so a sentence that was only   counts as the padding it is and is never sent.

/\A[[:space:]]*\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Segment

Returns a new instance of Segment.



9
10
11
12
13
# File 'lib/translation_diff/segment.rb', line 9

def initialize(source)
  @source = source.dup
  @leading, @body, @trailing = @source.partition(/[^[:space:]].*[^[:space:]]|[^[:space:]]/m)
  @core = TranslationDiff::Markup.decode_entities(@body)
end

Instance Attribute Details

#body ⇒ Object (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/translation_diff/segment.rb', line 3

def body
  @body
end

#core ⇒ Object (readonly)

Returns the value of attribute core.



3
4
5
# File 'lib/translation_diff/segment.rb', line 3

def core
  @core
end

#source ⇒ Object (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/translation_diff/segment.rb', line 3

def source
  @source
end

#translation ⇒ Object

Returns the value of attribute translation.



4
5
6
# File 'lib/translation_diff/segment.rb', line 4

def translation
  @translation
end

Instance Method Details

#empty? ⇒ Boolean

Returns:

  • (Boolean)


18
# File 'lib/translation_diff/segment.rb', line 18

def empty? = core.match?(BLANK)

#render ⇒ Object

Untranslated hands back the bytes it was cut from; a translation is text, so it is encoded as markup on the way out.



21
22
23
# File 'lib/translation_diff/segment.rb', line 21

def render
  "#{@leading}#{translated? ? escaped_translation : @body}#{@trailing}"
end

#translated? ⇒ Boolean

Reflects whether a translation is set right now, not history -- clearing it to nil flips this back to false.

Returns:

  • (Boolean)


16
# File 'lib/translation_diff/segment.rb', line 16

def translated? = !translation.nil?