Skip to content

Commit

Permalink
add sections into the document
Browse files Browse the repository at this point in the history
  • Loading branch information
dj2 committed Feb 22, 2011
1 parent 772c28e commit 66ef293
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/ruby-rtf/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Document
# @return [String] The characgter set for the document (:ansi, :pc, :pca, :mac)
attr_accessor :character_set

# @return [Array] The different formatted sections of the document
attr_accessor :sections

# Creates a new document
#
# @return [RubyRTF::Document] The new document
Expand All @@ -22,6 +25,25 @@ def initialize
@colour_table = []
@character_set = :ansi
@default_font = 0

@sections = [{:text => '', :modifiers => {}}]
end

def add_section!
return if current_section[:text].empty?

mods = {}
current_section[:modifiers].each_pair { |k, v| mods[k] = v } if current_section

@sections << {:text => '', :modifiers => mods}
end

def reset_section!
current_section[:modifiers] = {}
end

def current_section
@sections.last
end

# Convert RubyRTF::Document to a string
Expand All @@ -41,6 +63,11 @@ def to_s
str << " #{idx}: #{colour}\n"
end

str << " Body:\n\n"
sections.each do |section|
str << "#{section[:text]}\n"
end

str
end
end
Expand Down
55 changes: 55 additions & 0 deletions spec/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,59 @@
RubyRTF::Document.new.default_font.should == 0
end
end

context 'sections' do
it 'has sections' do
RubyRTF::Document.new.sections.should_not be_nil
end

it 'sets an initial section' do
RubyRTF::Document.new.current_section.should_not be_nil
end

context '#add_section!' do
it 'does not add a section if the current :text is empty' do
d = RubyRTF::Document.new
d.add_section!
d.sections.length.should == 1
end

it 'adds a section of the current section has text' do
d = RubyRTF::Document.new
d.current_section[:text] = "Test"
d.add_section!
d.sections.length.should == 2
end

it 'inherits the modifiers from the previous section' do
d = RubyRTF::Document.new
d.current_section[:modifiers] = {:bold => true, :italics => true}
d.current_section[:text] = "New text"

d.add_section!

d.current_section[:modifiers][:underline] = true

sections = d.sections
sections.first[:modifiers].should == {:bold => true, :italics => true}
sections.last[:modifiers].should == {:bold => true, :italics => true, :underline => true}
end
end

context '#reset_section!' do
it 'resets the current sections modifiers' do
d = RubyRTF::Document.new
d.current_section[:modifiers] = {:bold => true, :italics => true}
d.current_section[:text] = "New text"

d.add_section!
d.reset_section!
d.current_section[:modifiers][:underline] = true

sections = d.sections
sections.first[:modifiers].should == {:bold => true, :italics => true}
sections.last[:modifiers].should == {:underline => true}
end
end
end
end

0 comments on commit 66ef293

Please sign in to comment.