Module: FlexiRecordDemo

Defined in:
lib/flexirecord-demo.rb

Overview

Copyright © 2007 FlexiGuided GmbH, Berlin

Author: Jan Behrens

Website: www.flexiguided.de/publications.flexirecord.en.html


Demonstration module for FlexiRecord.

Defined Under Namespace

Classes: Medium, MediumEntry, Movie, Person, Rating

Constant Summary collapse

ConnectionPool =

FlexiRecord::ConnectionPool used for all models in this module.

FlexiRecord::BaseRecord.connection_pool = FlexiRecord::ConnectionPool.new(:engine => :postgresql, :db => 'moviedemo')

Class Method Summary collapse

Class Method Details

.demoObject

A small demonstration program. In order to be run, a database named ‘moviedemo’ has to be installed and initialized with the ‘flexirecord-demo.sql’ file, which is shipped with the software package.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/flexirecord-demo.rb', line 94

def demo
  # Creating demo entries
  Person.transaction do
    Rating.db_execute(     "DELETE FROM #{Rating.table}"     )
    MediumEntry.db_execute("DELETE FROM #{MediumEntry.table}")
    Movie.db_execute(      "DELETE FROM #{Movie.table}"      )
    Medium.db_execute(     "DELETE FROM #{Medium.table}"     )
    Person.db_execute(     "DELETE FROM #{Person.table}"     )
  end
  anja    = Person.new(:name => 'Anja'   ).save
  phillip = Person.new(:name => 'Phillip').save
  wilson  = Person.new(:name => 'Wilson' ).save
  american_beauty = Movie.new(:name => 'American Beauty').save
  naruto          = Movie.new(:name => 'Naruto').save
  koyaanisqatsi   = Movie.new(:name => 'Koyaanisqatsi').save
  medium_a = nil
  Medium.transaction do
    medium_a = Medium.new(:number => :auto).save
    FlexiRecordDemo::MediumEntry.new(:medium => medium_a, :position => :last, :movie => naruto).save
  end
  medium_b = nil
  Medium.transaction do
    medium_b = Medium.new(:number => '42', :lent_to => phillip).save
    MediumEntry.new(:medium => medium_b, :position => :last, :movie => koyaanisqatsi).save
    MediumEntry.new(:medium => medium_b, :position => :last, :movie => american_beauty).save
  end
  Rating.new(:person => anja, :movie => american_beauty, :rating => Rational(7, 10)).save
  Rating.new(:person => anja, :movie => koyaanisqatsi, :rating => Rational(9,10)).save
  Rating.new(:person => phillip, :movie => koyaanisqatsi, :rating => Rational(6,10)).save
  Rating.new(:person => phillip, :movie => koyaanisqatsi, :rating => Rational(8,10)).save
  Rating.new(:person => wilson, :movie => naruto, :comment => 'Rasengan!').save
  # Some queries
  person = Person.select1('WHERE "name" ILIKE $ ORDER BY "name" DESC LIMIT 1', 'P%')
  puts "First person whose name is starting with 'P' is: #{person.name}."
  puts "The following media are borrowed by him/her:"
  person.borrowed_media.each do |medium|
    puts "- ##{medium.number}"
    medium.entries.each do |entry|
      puts "  - #{entry.movie.name}"
    end
  end
  puts "He rated the following movies:"
  person.rated_movies.each do |movie|
    rating = movie.rel
    puts "- #{movie.name}"
    puts "  - Rating: #{rating.rating ? rating.rating.to_s : 'none'}"
    puts "  - Comment: #{rating.comment || 'none'}"
  end
  anjas = Person.select('WHERE "name" = $', 'Anja')
  if anjas.length == 1
    anja = anjas.first
    anjas_favourite = anja.rated_movies('WHERE "rel"."rating" NOTNULL ORDER BY "rel"."rating" DESC LIMIT 1').first
    puts "The movie, which Anja likes most is: #{anjas_favourite ? anjas_favourite.name : 'N/A'}."
  else
    puts "There is more than one person named Anja."
  end
  nil
end