Class: CSKit::AnnotatedString

Inherits:
Object
  • Object
show all
Defined in:
lib/cskit/annotated_string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, annotations = []) ⇒ AnnotatedString

Returns a new instance of AnnotatedString.



9
10
11
12
# File 'lib/cskit/annotated_string.rb', line 9

def initialize(string, annotations = [])
  @string = string
  @annotations = annotations
end

Instance Attribute Details

#annotationsObject (readonly)

Returns the value of attribute annotations.



7
8
9
# File 'lib/cskit/annotated_string.rb', line 7

def annotations
  @annotations
end

#stringObject (readonly)

Returns the value of attribute string.



6
7
8
# File 'lib/cskit/annotated_string.rb', line 6

def string
  @string
end

Instance Method Details

#add_annotation(annotation) ⇒ Object



14
15
16
# File 'lib/cskit/annotated_string.rb', line 14

def add_annotation(annotation)
  annotations << annotation
end

#delete(pos, length) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cskit/annotated_string.rb', line 28

def delete(pos, length)
  new_length = string.length - length

  annotations.reject! do |annotation|
    reject = annotation.start >= new_length || annotation.finish >= new_length

    if annotation.start >= pos
      annotation.start -= length
      annotation.finish -= length
    end

    reject || (annotation.start < 0 || annotation.finish < 0)
  end

  @string = @string[0...pos] + @string[(pos + length)..-1]
end

#renderObject



18
19
20
21
22
23
24
25
26
# File 'lib/cskit/annotated_string.rb', line 18

def render
  each_chunk.map do |text, annotation|
    if annotation
      yield text, annotation
    else
      text
    end
  end.join
end