# Create Api to create document and add,edit,delete text and t

**URL:** <https://www.ruby-forum.com/t/create-api-to-create-document-and-add-edit-delete-text-and-t/178908>\
**Category:** Ruby\
**Created:** [November 13, 2009, 11:06am UTC](https://www.ruby-forum.com/t/create-api-to-create-document-and-add-edit-delete-text-and-t/178908 "2009-11-13T11:06:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![skrithikaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@skrithikaa](https://www.ruby-forum.com/u/skrithikaa)\
**Post date:** [November 13, 2009, 11:06am UTC](https://www.ruby-forum.com/t/create-api-to-create-document-and-add-edit-delete-text-and-t/178908/1 "2009-11-13T11:06:48Z")

</div>

Sub: Create Api to create document and add, edit, delete text and  
table(add row, modify and delete row)

Can somebody please tell me how to implement this using inheritence /  
composition / patterns ?

Thanks,  
skrithikaa

---

<div class="post-metadata">

**Author:** ![skrithikaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@skrithikaa](https://www.ruby-forum.com/u/skrithikaa)\
**Post date:** [November 14, 2009, 5:46am UTC](https://www.ruby-forum.com/t/create-api-to-create-document-and-add-edit-delete-text-and-t/178908/2 "2009-11-14T05:46:06Z")

</div>

Krithika San wrote:

> Sub: Create Api to create document and add, edit, delete text and  
> table(add row, modify and delete row)
> 
> Can somebody please tell me how to implement this using inheritence /  
> composition / patterns ?

Do your own homework. 🙂 If you’re stuck, let us know what you’ve tried  
and what isn’t working.

> Thanks,  
> skrithikaa

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![skrithikaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@skrithikaa](https://www.ruby-forum.com/u/skrithikaa)\
**Post date:** [November 14, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/create-api-to-create-document-and-add-edit-delete-text-and-t/178908/3 "2009-11-14T21:48:30Z")

</div>

This is what I coded and I need to change so that it accepts the text,  
and table rows and columns in a dynamic way.

This was the comments I obtained. Can you please correct the mistake.

Table and Text are not “is-a” DocumentBuilder  
It is not clear how TableElt and TextElt contribute to the requirements  
The DataTable class (which appears to be how you addressed the Table  
requirements) is not related to the DocumentBuilder stuff. In fact, I’m  
not sure how to create a document with a working table in it at all  
Also, I don’t see how to add a Text element to a document

Please put your classes in separate .rb files in the future.  
The design document includes a lot of boilerplate text about the  
components of an abstract factory, but it is not clear to me how any of  
this relates to fulfilling the requirements of the core classes in the  
assignment.  
The “alter” method should just accept the row/column to “alter” rather  
than have to set them prior.

Hope you can help me with this.

class DocumentBuilder  
def new\_document  
end  
end

class Table \< DocumentBuilder  
end

class Text \< DocumentBuilder  
end

# 2. implementations of specializations

class TableElt \< Table  
def new\_document  
puts "I am a word document "  
end  
end

class TextElt \< Text  
def new\_document  
puts “I am a word document”  
end  
end

# 3. interface for factory (abstract factory)

class DocumentBuilderFactory  
def create\_doc  
end  
end

# 4. different factories implementations

class TableFactory \< DocumentBuilderFactory  
def create\_doc  
TableElt.new  
end  
end

class TextFactory \< DocumentBuilderFactory  
def create\_doc  
TextElt.new  
end  
end

class DynamicFactory  
def initialize(doc\_class)  
@doc\_class = doc\_class  
end

def create\_doc  
@doc\_class.new  
end  
end

# 5. factories manager (optional)

class DocumentBuilderManager  
def create\_doc\_factory(type)  
if(type == :Table)  
TableFactory.new  
elsif(type == :Text)  
TextFactory.new  
end  
end  
end

# 6. test

doc\_manager = DocumentBuilderManager.new

DocumentBuilder\_factory1 = doc\_manager.create\_doc\_factory(:Table)  
DocumentBuilder\_factory2 = doc\_manager.create\_doc\_factory(:Text)  
DocumentBuilder\_factory3 = DynamicFactory.new(TableElt)

DocumentBuilder1 = DocumentBuilder\_factory1.create\_doc  
DocumentBuilder2 = DocumentBuilder\_factory2.create\_doc  
DocumentBuilder3 = DocumentBuilder\_factory3.create\_doc

DocumentBuilder1.new\_document  
DocumentBuilder2.new\_document  
DocumentBuilder3.new\_document

# creating table entries

class DataTable  
attr\_accessor :current\_line, :current\_column  
attr\_reader :file\_name

# A table is basically an array of arrays.

# So start by creating the host array.

def initialize  
@table = Array.new  
@current\_line = 0  
@current\_column = 0  
end

# Allows to add elements into the table

def add\_line(\*entries)  
@table \<\< entries  
end

# Alters the content in a table

# (note first line = 0, and first cell = 0)

def alter\_current\_cell(entry)  
@table[@current\_line][@current\_column] = entry  
end

# Deletes a row in a table

def remove\_line(line\_number)  
@table.delete\_at(line\_number)  
end

# Create an output file

def output\_to\_file(file\_name)  
@file\_name = file\_name  
create\_file  
put\_data\_into\_file  
end

private  
def create\_file  
File.delete(@file\_name) if File.exist?(@file\_name)  
File.new(@file\_name, ‘w+’)  
end

# Add the table info, and close

def put\_data\_into\_file  
output\_file = File.open(@file\_name, ‘w+’)  
for line in @table  
output\_file.puts line.join(" ")  
end  
output\_file.close  
end  
end

data\_table = DataTable.new  
data\_table.add\_line(‘Country’, ‘Animal’, ‘Fruit’)  
data\_table.add\_line(‘India’, ’ Tiger ‘, ’ Mango’)  
data\_table.add\_line(‘China’, ’ Cheeta ‘, ‘Apple’)  
data\_table.add\_line(‘Japan’,’ Monkey ‘,‘Orange’)  
puts “View display\_table.txt to see the contents of the table”  
data\_table.output\_to\_file(‘display\_table.txt’)  
data\_table.current\_line = 2 # entry in this cell is changed  
data\_table.current\_column = 1 # entry in this cell is changed  
data\_table.alter\_current\_cell(’ DONKEY ') # entry in this cell is  
changed to DONKEY  
puts “View modified\_ouput.txt to see modified content”  
data\_table.output\_to\_file(‘modified\_ouput.txt’)  
data\_table.current\_line = 1  
data\_table.current\_column = 2  
data\_table.remove\_line(1)  
puts “View output\_without\_line\_1.txt to see the final output with the  
removal of row 1”  
data\_table.output\_to\_file(‘output\_without\_line\_1.txt’)

Marnen Laibow-Koser wrote:

> Krithika San wrote:
> 
> > Sub: Create Api to create document and add, edit, delete text and  
> > table(add row, modify and delete row)
> > 
> > Can somebody please tell me how to implement this using inheritence /  
> > composition / patterns ?
> 
> Do your own homework. 🙂 If you’re stuck, let us know what you’ve tried  
> and what isn’t working.
> 
> > Thanks,  
> > skrithikaa
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![skrithikaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@skrithikaa](https://www.ruby-forum.com/u/skrithikaa)\
**Post date:** [November 14, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/create-api-to-create-document-and-add-edit-delete-text-and-t/178908/4 "2009-11-14T22:28:50Z")

</div>

Krithika San wrote:

> This is what I coded and I need to change so that it accepts the text,  
> and table rows and columns in a dynamic way.
> 
> This was the comments I obtained. Can you please correct the mistake.

No! The comments provide all the information you need for you to do  
your own correcting. Try it yourself first. If you’re stuck, read a  
reference book, search the Web, or ask your professor. If you have a  
specific question to ask, most of us here will be happy to answer it,  
but simply dumping a homework assignment that you obviously haven’t even  
_tried_ to work on is not OK.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
