Class: TextWeaver

Inherits:
Object
  • Object
show all
Defined in:
lib/text_weaver.rb,
lib/text_weaver/version.rb

Defined Under Namespace

Classes: Error, InsertPosition

Constant Summary collapse

VERSION =
'0.6.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ TextWeaver

Initializes a new TextWeaver.



28
29
30
31
# File 'lib/text_weaver.rb', line 28

def initialize(text)
  self.text = text
  self.clear
end

Instance Attribute Details

#textObject

Attributes



22
23
24
# File 'lib/text_weaver.rb', line 22

def text
  @text
end

Instance Method Details

#add_closing(position, text) ⇒ Object

Adds stuff to the weaver.

See add_opening

For closing-tags.



64
65
66
67
# File 'lib/text_weaver.rb', line 64

def add_closing(position, text)
  check_position(position)
  @insert_positions << TextWeaver::InsertPosition.new(position, @set * -1, text)
end

#add_opening(position, text) ⇒ Object

Adds stuff to the weaver.

This makes sure that different kinds of tags are kept together (opening and closing-tags in order. If are set 1, and set 2, then overlapping ‘s and ’s are kept in order, so <b>the <i>cow, instead of the cow).

For opening-tags.



53
54
55
56
# File 'lib/text_weaver.rb', line 53

def add_opening(position, text)
  check_position(position)
  @insert_positions << TextWeaver::InsertPosition.new(position, @set, text)
end

#next_setObject

Increases the set-counter…

(the thing that keeps tag-types apart)



39
40
41
# File 'lib/text_weaver.rb', line 39

def next_set
  @set += 1
end

#weaveObject

Weaves in all stuff added via add_opening and add_closing.

Returns the text with everything woven in.

(self.text remains unmodified)



75
76
77
78
79
80
81
82
# File 'lib/text_weaver.rb', line 75

def weave
  weave_text = self.text.dup
  @insert_positions.sort!
  @insert_positions.reverse.each do |i_p|
    weave_text.insert(i_p.position, i_p.text)
  end
  return weave_text
end