accessing one class from another class

N

Nick da G

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
 
N

nick ger

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.
 
N

nick ger

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
 
A

Alex Eiras

[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/tree/master

happy-mapper:
http://happymapper.rubyforge.org/

xml-mapping:
http://xml-mapping.rubyforge.org/

Hope it helps
Cheers
 
N

nick ger

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.
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top