Skip to content

Instantly share code, notes, and snippets.

@tngn
Last active April 15, 2016 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tngn/7970489 to your computer and use it in GitHub Desktop.
Save tngn/7970489 to your computer and use it in GitHub Desktop.
A revised version of hw6_rottenpotatoes/db/seeds.rb for better control of the numbers of records generated. Syntax: rake db:seed [options] with options being one of (show, max, debug): show: displays the current numbers of movie, moviegoer, and review records; max=nn: generate up to nn movie records, nn moviegoers, and 20*nn reviews; generations…
=begin
This file should contain all the record creation needed to seed the database with its default values.
The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
Examples:
movies = [{:title => 'Aladdin', :rating => 'G', :release_date => '25-Nov-1992'},
{:title => 'The Terminator', :rating => 'R', :release_date => '26-Oct-1984'},
# ...
{:title => 'Chicken Run', :rating => 'G', :release_date => '21-Jun-review_count00'},
]
movies.each do |movie|
Movie.create!(movie)
end
=end
require 'debugger'
require 'io/console'
def choose(msg='')
printf "%s? [y]/n " % msg if not msg.blank?
r=$stdin.readline.strip
(r.empty? or not (r.match /y/i).nil?) # outermost parentheses are a MUST
# r.empty? or not (r.match /y/i).nil? ==> this evaluates to false! when r=='y'
end
def mkMovies(movie_count, movie_ids=[])
mod_count=movie_count / 10 # 10% progress indicator
movie_count.times do |i|
time=Time.now
movie = Movie.create!(:title => "Movie_#{i}", :release_date => time, :rating => "G")
movie_ids << movie.id
printf '=' if (i % mod_count)==0
end
puts "\n#{movie_count} movies created"
return movie_ids
end
def mkGoers_n_Reviews(movie_count, review_count, movie_ids=[])
curr_movie_count=movie_ids.size - movie_count
mod_count=movie_count / 10 # 10% progress indicator
movie_count.times do |i|
goer = Moviegoer.create!(:name => "Person_#{i}")
review_count.times do |j|
# should give each (number of) movies an equal number of reviews
movie_id = movie_ids[curr_movie_count+((i+j) % movie_count)]
Review.create!(:movie_id => movie_id, :moviegoer_id => goer.id, :score => 3)
end
printf '=' if (i % mod_count)==0
end
puts("\nNumber of reviews created is : #{review_count*movie_count}")
end
rx=/show/=~ARGV[1]
if (not rx.nil?)
printf "Current number of Movies: %i\n" % Movie.count
printf "Current number of Moviegoer: %i\n" % Moviegoer.count
printf "Current number of Reviews: %i\n" % Review.count
exit
end
rx=/debug/=~ARGV[1]
debug = (not rx.nil?)
# puts 'Press q to quit'
debugger if debug
review_count=20
movie_count=250
stopcount = nil
/max=*(?<stopcount>\d*)/=~ARGV[1]
countstop=(not stopcount.nil?)
movie_count=stopcount.to_i if countstop
movie_ids = mkMovies(movie_count) if (not countstop) or choose('Make movies')
movie_ids ||=Movie.select(:id).pluck(:id)
mkGoers_n_Reviews(movie_count, review_count, movie_ids) if (not countstop) or choose('Make goers and reviews')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment