From 66ef293ea75b61c18ccdcef2e54e9ddfa948a0bc Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 21 Feb 2011 19:48:37 -0500 Subject: [PATCH] add sections into the document --- lib/ruby-rtf/document.rb | 27 ++++++++++++++++++++ spec/document_spec.rb | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/lib/ruby-rtf/document.rb b/lib/ruby-rtf/document.rb index c717eef..b6f209a 100644 --- a/lib/ruby-rtf/document.rb +++ b/lib/ruby-rtf/document.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/document_spec.rb b/spec/document_spec.rb index 7017100..dc9d306 100644 --- a/spec/document_spec.rb +++ b/spec/document_spec.rb @@ -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 \ No newline at end of file