NAME
MyLibrary::Review
SYNOPSIS
# use the module
use MyLibrary::Review;
# create a new review
my $review = MyLibrary::Review->new;
# give the review characteristics
$review->review('This resource worked just fine more me.');
$review->reviewer_name('Fred Kilgour');
$review->reviewer_email('kilgour@oclc.org');
$review->review_date('2002-10-31');
$review->review_rating('3');
# associate the review with a resource
$review->resource_id(601);
# save the review; create a new record or update it
$review->commit;
# get the id of the current review object
$id = $review->review_id;
# create a new review object based on an id
my $review = MyLibrary::Review->new(id => $id);
# display a review
print ' Resource ID: ', $review->resource_id, "\n";
print ' Review: ', $review->review, "\n";
print ' Reviewer: ', $review->reviewer_name, "\n";
print ' Email: ', $review->reviewer_email, "\n";
print ' Rating: ', $review->review_rating, "\n";
print ' Date: ', $review->review_date, "\n";
DESCRIPTION
The module provides a means of saving reviews of information resources to the underlying MyLibrary database.
METHODS
This section describes the methods available in the package.
new
Use this method to create a new review object. Called with no arguments, this method creates an empty object. Given an id, this method gets the review from the database associated accordingly.
# create a new review object
my $review = MyLibrary::Review->new;
# create a review object based on a previously existing ID
my $review = MyLibrary::Review->new(id => 3);
review_id
This method returns an integer representing the database key of the currently created review object.
# get id of current review object
my $id = $review->review_id;
You cannot set the review_id attribute.
review
This method gets and sets the text of the review for the current review object:
# get the text of the current review object
my $text = $review->review;
# set the current review object's text
$review->review('I would recommend this resoruce to anyone.');
reviewer_name
Use this method to get and set the name of a review's reviewer:
# get the reviewer's name
my $reviewer = $review->reviewer_name;
# set the reviwer's name
$librarian->reviewer_name('Paul Evan Peters');
reviewer_email
Usse this method to get and set the reviewer's email address of the review object:
# get the email address
my $email_address = $review->reviewer_email;
# set the email address
$review->reviewer_email('pep@greatbeyond.org');
date
Set or get the date attribute of the review object with this method:
# get the date attribute
my $review_date = $review->review_date;
# set the date
$review->review_date('2003-10-31');
The date is expected to be in the format of YYYY-MM-DD.
review_rating
Use this method to set a rating in the review.
# set the rating
$review->review_rating('3');
# get rating
my $review_rating = $review->review_rating;
Ratings can be strings up to 255 characters in length, but this attribute is intended to be an integer value for calculating purposes. The programer can use the attribute in another manner if they so choose.
resource_id
Use this method to get and set what resource is being reviewed:
# set the resource
$review->resource_id('601');
# get resource id
my $resource_id = $review->resource_id;
commit
Use this method to save the review object's attributes to the underlying database. If the object's data has never been saved before, then this method will create a new record in the database. If you used the new and passed it an id option, then this method will update the underlying database.
This method will return true upon success.
# save the current review object to the underlying database
$review->commit;
delete
This method simply deletes the current review object from the underlying database.
# delete (drop) this review from the database
$review->delete();
get_reviews
Use this method to get all the reviews from the underlying database. It method returns an array of objects enabling you to loop through each object in the array and subsequent characteristics of each object;
# get all reviews
my @reviews = MyLibrary::Review->get_reviews;
# initialize counters
my $total_rating = 0;
my $total_reviews = 0;
# process each review
foreach my $r (@reviews) {
# look for a particular resource
if ($r->resource_id == 601) {
# update counters
$total_rating = $total_rating + $r->review_rating;
$total_reviews = $total_reviews + 1;
}
}
# check for reviews
if ($total_reviews) {
# print the average rating
print "The average rating for resource 601 is: " . ($total_rating / $total_reviews)
}
AUTHOR
Eric Lease Morgan <emorgan@nd.edu>
HISTORY
October 31, 2003 - first public release; Halloween