[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

accessing one class from another class

Nick da G

5/4/2009 9:39:00 PM

Hello, All.

I'm trying to consuming an xml file and map it to a Ruby object that
would describe that xml file.

What would be the best way to implement the following scenario.

Let's say I have several classes: Student, Course, Person, Tuition
Xml for this would look like this:
<Record>
<Student status="good">
<Person>
<Name>Alice</Name>
<BirthPlace>WonderLand</BirthPlace>
</Person>
<Course difficulty=">
<CourseName>Math III</CourseName>
<Grade>B+</Grade>
</Course>
<dt:Tuition>
<dt:Currency>USD</dt:Currency>
<dt:Amount>500</dt:Amount>
<dt:Period>3 months</dt:Period>
</dt:Tuition>
</Student>
</Record>
So basically I can parse the xml file using rexml and access all of my
xml tags and stuff.

Here is the Object I want to map it to:


class Student
@status
@person - access Person
end

class Person
end

class Course
end

class Tution
end



So the question is how do I access from class Student - class Person? So
that when I down the line later when I parse the file and do:

r = Record.new

I want to be able to do things like:
r.student.person.name

????


Thank you
--
Posted via http://www.ruby-....

5 Answers

Nick da G

5/4/2009 9:43:00 PM

0

Also to clarify xml tag like <Course> can be repeated in my xml file,
so:

r.student.course
could return an array of objects of class Course.
--
Posted via http://www.ruby-....

Nick da G

5/4/2009 10:05:00 PM

0

To clarify Futhre I'm doing somethign like this right now:

class Base

attr_accessor :name, :value

def initialize
@name
@value
end
end

class Student
attr_accessor :person, :tuition

def initialize
@person = Person.new
@tuition = Tuition.new
end

end

... define Tuition & Person class. I'm just looking for a better way to
do it.

Also how would I properly define :course within Student, considering
that I might have a single Course or multiple - depending on the record.

Thank you
--
Posted via http://www.ruby-....

Alex Eiras

5/4/2009 10:07:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

> Also to clarify xml tag like <Course> can be repeated in my xml file,
> so:
>
> r.student.course
> could return an array of objects of class Course.
> --
>


Hi Nick,

There are some good libraries to accomplish this. You may want to have a
look at one of these (ordered by my own preferences):

xml-object:
http://github.com/jordi/xml-object/t...

happy-mapper:
http://happymapper.ruby...

xml-mapping:
http://xml-mapping.ruby...

Hope it helps
Cheers

Nick da G

5/4/2009 10:40:00 PM

0

Thank you Alex, but no go :-(

I looked into all of them - they all lack in some respect:
happy-mapper - can't build xml & handle namespaces
xml-object can't handle namespaces - and some other issue - can't
remember
xml-mapping - same thing
roxml - same problem.

So the solution is to just do it myself. However that wasn't my question
- it was more a of generic ruby class question.
--
Posted via http://www.ruby-....

Robert Klemme

5/5/2009 6:25:00 AM

0

On 04.05.2009 23:39, Nick da G wrote:
> Hello, All.
>
> I'm trying to consuming an xml file and map it to a Ruby object that
> would describe that xml file.
>
> What would be the best way to implement the following scenario.
>
> Let's say I have several classes: Student, Course, Person, Tuition
> Xml for this would look like this:
> <Record>
> <Student status="good">
> <Person>
> <Name>Alice</Name>
> <BirthPlace>WonderLand</BirthPlace>
> </Person>
> <Course difficulty=">
> <CourseName>Math III</CourseName>
> <Grade>B+</Grade>
> </Course>
> <dt:Tuition>
> <dt:Currency>USD</dt:Currency>
> <dt:Amount>500</dt:Amount>
> <dt:Period>3 months</dt:Period>
> </dt:Tuition>
> </Student>
> </Record>
> So basically I can parse the xml file using rexml and access all of my
> xml tags and stuff.
>
> Here is the Object I want to map it to:
>
>
> class Student
> @status
> @person - access Person
> end
>
> class Person
> end
>
> class Course
> end
>
> class Tution
> end
>
>
>
> So the question is how do I access from class Student - class Person? So
> that when I down the line later when I parse the file and do:
>
> r = Record.new
>
> I want to be able to do things like:
> r.student.person.name

You can define attributes via attr_accessor, e.g.

class Student
attr_accessor :person
end

class Person
attr_accessor :name
end

st = Student.new
pe = Person.new
pe.name = "Nick"
st.person = pe

etc.

As a shortcut you can as well use Struct:

Student = Struct.new :person
Person = Struct.new :name

Kind regards

robert