Project 6.2

Apr 9, 2012

Scenario

"What the heck am I doing here?" you think, barely able to hear your thoughts over the loud, thumping industrial-goth music in the dark club. People are moving in the shadows all around you, and the lights are mesmerizing. It's been quite a week... while other students were partying it up for "Spring Break 2010 woo!", you've been questioning reality and being chased by water-gun carrying clowns with bad senses of humor.

After being forced to munge SQL strings and program with low-level database functions, you were apparently knocked unconscious only to wake up in your bed (in a wet spot, gross!). And before you could even collect your thoughts (and burn your mattress in the back yard) and figure out if it was all a dream or not, you heard the familiar ringing of that grey banana-shaped phone that arrived in the mail. "Yes, this is really happening to me," you think, as you watch the dark, pierced crowd continue to dance around you.

"They got to you first, but they've underestimated who you are," you recall the voice saying. "We need to meet -- follow the white rabbit." After following the instructions which led you to this warehouse-turned-dance-club, you wonder... could all this be a joke?

"I'm glad you're here, Seekwell" shouts a dark figure who emerges from the shadows. He or she (you can't quite tell) smiles and takes your hand. You immediately notice the person is clad in skin-tight, black leather and is literally dripping with sweat. "Morpheus would like to see you now, come with me. My goodness, this outfit doesn't breath at all!" You follow your apparent guide up an iron, spiral staircase.

"Wow, it's much quieter up here. Say, what's your name?" you ask your guide, trying to break the ice. He doesn't answer, and you continue to follow in silence. He opens a set of huge double doors at the end of a corridor, which opens to a small room, and motions for you to enter.

"Ah, Seekwell, at last we meet!" says a large friendly-looking man. "I am Morpheus. I take it you were able to follow Sarah here?"

"Who?"

"Sarah, the girl with the tattoo who knocked on your door at home. Did she not find you?"

"What do you mean? You told me to 'follow the white rabbit' and I saw one out the kitchen window and followed it. I barely had time to grab my laptop before it started hopping away down the street."

"Do you mean to say that you actually followed a white rabbit here? That's amazing! This virtual reality we call The Mines never ceases to amaze me. Now about The Mines..." Morpheus tells you an incredible tale of how The Mines was created and how your mind is really being fed a virtual sensory experience while your body sits in a huge Twinkie with wires drilled into your head. You always were suspicious of reality and now finally your doubts have been confirmed!

"And so now, Seekwell, you must make a decision," says Morpheus in a grave tone. "Unfortunately, no one can be told what The Mines is. You have to see it for yourself." Morpheus holds out a red pill in one hand and a blue pill in another. "This is your last chance. After this, there is no going back. You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe. It's also blueberry flavored, so that's kinda nice. However, take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes. I'm speaking metaphorically, so don't think we're going to actually go down a rabbit hole," he smirks. "Remember, all I am offering you is the truth (and the ability to program with databases without directly using SQL). Nothing more."

Requirements

This step in project 6.2 will conclude our brief tour of ORMs. You will implement additional classes, establish relationships between those classes, and add a few constraints.

Continuing the Project

Continue working in a subdirectory of cs403 called project06. This is your dedicated working directory for all 6.x project assignments.

At this point, you should have added a simple User class to your zoolicious.rb file and used the class to load User data from the database and display it.

We're now going to implement a simple menu-driven console application that displays additional data and allows the user to insert a new animal into the database.

In order to accomplish this, leverage the features of the ActiveRecord ORM to make your code very lightweight. You will need to declare classes for all of the entities in the database; declare relationships in those class definitions; and declare a few constraints in the Animal class. You'll also need to implement a handful of functions that use the classes that you create.

First, download this zoolicious.rb.txt and save it as an .rb file. Now run it. You should see a simple menu like this:

ZOOOOOLICIOUS Application Simulator. Whee!
Main Menu
A. List Zoos
B. List Feeds
C. List Habitats
D. List Animals
E. List Users
F. Add a New Animal
Q. Quit

Play around with the program. You'll notice it doesn't do a ton -- this is your task.

Open zoolicious.rb in an editor and take a look at the code. Some functionality has been provided for you, but you must implement the functions whose job it is to interact with the database and display some data (and insert some new data). These functions have been tagged with a # TODO comment.

Note: pay attention to the comment that precedes each function you are to implement. You must fulfill the functionality described. For example, when listing users, you might print:

Listing Users
Edgar Codd (efcodd)
Animals: Puff the Magic Dragon, The Gopher from CaddyShack
Chris Date (cjdate)
Animals: Leprechaun, Stay-Puf Marshmallow Man
John Smith (jsmith)
Animals: Trigger the Horse, Arnold the Ant

How you display this data is up to you. You must print at least one attribute (eg, name) for each entity that is displayed.

There is one last requirement: since MySQL does not allow us to enforce a CHECK constraint, you must make sure that the Animal's cuteness attribute is between 1 and 100 (inclusively). Hint: Do not validate the user input -- see how the ActiveRecord ORM allows you to declare "validations" which are really attribute constraints.

You do not need to spend time handling the cases where the object is not saved due to a validation failing. As long as "no Animal with a cuteness outside of the range 1-100 is saved to the database," then you're all set.

Grading Criteria (500 pts)

Copy and paste the body of your zoolicious.rb into blackboard (do not attach).

This is due by 11:59PM on Tuesday, Apr 17.