Skip to content

Instantly share code, notes, and snippets.

@jisraelsen
Created June 26, 2009 23:14
Show Gist options
  • Save jisraelsen/136813 to your computer and use it in GitHub Desktop.
Save jisraelsen/136813 to your computer and use it in GitHub Desktop.
movie import code used during URUG Rails Workshop Summer '09 (with supporting files)
require 'csv'
require 'rexml/document'
class Person
@@counter = 0
@@people = []
attr_reader :id, :name
def self.find_or_initialize(name)
if person = @@people.find { |p| p.name == name }
person
else
new(name)
end
end
def initialize(name)
@name = name
@id = @@counter += 1
@@people << self
end
def to_s
name
end
end
class Movie
attr_accessor :title, :director, :rating, :genre, :actors
def self.import(options)
if options[:csv]
import_from_csv(options[:csv])
elsif options[:xml]
import_from_xml(options[:xml])
end
end
def initialize(title, director, rating, genre, actors=[])
self.title = title
self.director = director
self.rating = rating
self.genre = genre
self.actors = actors
end
def to_s
"#{title}: #{director.name} [#{rating}] (#{genre})"
end
private
def self.import_from_csv(filepath)
movies = []
CSV.open(filepath, 'r') do |row|
next if row[0] == "Title"
director = Person.find_or_initialize(row[1])
actors = row[4].split("|").map { |name| Person.find_or_initialize(name) }
movies << Movie.new(row[0], director, row[2], row[3], actors)
end
movies
end
def self.import_from_xml(filepath)
movies = []
doc = REXML::Document.new(File.read(filepath))
doc.elements.each('movies/movie') do |m|
director = Person.find_or_initialize(m.elements['director'].text)
actors = []
m.elements.each('actors/actor') do |a|
actors << Person.find_or_initialize(a.text)
end
movies << Movie.new(m.elements['title'].text, director, m.elements['rating'].text, m.elements['genre'].text, actors)
end
movies
end
end
puts Movie.import(:csv => "movies.csv").inspect
puts
puts Movie.import(:xml => "movies.xml").join("\n")
Title Director Rating Genre Actors
Star Wars: Episode IV - A New Hope George Lucas PG SciFi Mark Hamill|Harrison Ford|Carrie Fisher
Star Wars: Episode V - The Empire Strikes Back George Lucas PG SciFi Mark Hamill|Harrison Ford|Carrie Fisher|Billy Dee Williams
Star Wars: Episode VI - Return of the Jedi George Lucas PG SciFi Mark Hamill|Harrison Ford|Carrie Fisher|Billy Dee Williams
Iron Man Jon Favreau PG-13 Action/Adventure Robert Downey Jr.|Terrence Howard|Jeff Bridges|Gwyneth Paltrow
Indiana Jones and the Raiders of the Lost Ark Steven Spielberg PG Action/Adventure Harrison Ford|Karen Allen
Indiana Jones and the Last Crusade Steven Spielberg PG-13 Action/Adventure Harrison Ford|Sean Connery
<?xml version="1.0" encoding="UTF-8"?>
<movies>
<movie>
<title>Star Wars: Episode IV - A New Hope</title>
<director>George Lucas</director>
<rating>PG</rating>
<genre>SciFi</genre>
<actors>
<actor>Mark Hamill</actor>
<actor>Harrison Ford</actor>
<actor>Carrie Fisher</actor>
</actors>
</movie>
<movie>
<title>Star Wars: Episode V - The Empire Strikes Back</title>
<director>George Lucas</director>
<rating>PG</rating>
<genre>SciFi</genre>
<actors>
<actor>Mark Hamill</actor>
<actor>Harrison Ford</actor>
<actor>Carrie Fisher</actor>
<actor>Billy Dee Williams</actor>
</actors>
</movie>
<movie>
<title>Star Wars: Episode VI - Return of the Jedi</title>
<director>George Lucas</director>
<rating>PG</rating>
<genre>SciFi</genre>
<actors>
<actor>Mark Hamill</actor>
<actor>Harrison Ford</actor>
<actor>Carrie Fisher</actor>
<actor>Billy Dee Williams</actor>
</actors>
</movie>
<movie>
<title>Iron Man</title>
<director>Jon Favreau</director>
<rating>PG-13</rating>
<genre>Action/Adventure</genre>
<actors>
<actor>Robert Downey Jr.</actor>
<actor>Terrence Howard</actor>
<actor>Jeff Bridges</actor>
<actor>Gwyneth Paltrow</actor>
</actors>
</movie>
<movie>
<title>Indiana Jones and the Raiders of the Lost Ark</title>
<director>Steven Spielberg</director>
<rating>PG</rating>
<genre>Action/Adventure</genre>
<actors>
<actor>Harrison Ford</actor>
<actor>Karen Allen</actor>
</actors>
</movie>
<movie>
<title>Indiana Jones and the Last Crusade</title>
<director>Steven Spielberg</director>
<rating>PG-13</rating>
<genre>Action/Adventure</genre>
<actors>
<actor>Harrison Ford</actor>
<actor>Sean Connery</actor>
</actors>
</movie>
</movies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment