-
-
Save universal/7101325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2.0.0-p247 :019 > m1 = Movie.create start_time: Time.parse("10:00"), end_time: Time.parse("10:30") | |
(0.1ms) begin transaction | |
Movie Load (0.2ms) SELECT "movies".* FROM "movies" | |
SQL (0.8ms) INSERT INTO "movies" ("created_at", "end_time", "start_time", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 22 Oct 2013 14:00:43 UTC +00:00], ["end_time", 2013-10-22 10:30:00 +0200], ["start_time", 2013-10-22 10:00:00 +0200], ["updated_at", Tue, 22 Oct 2013 14:00:43 UTC +00:00]] | |
(20.2ms) commit transaction | |
=> #<Movie id: 7, start_time: "2013-10-22 10:00:00", end_time: "2013-10-22 10:30:00", created_at: "2013-10-22 14:00:43", updated_at: "2013-10-22 14:00:43"> | |
2.0.0-p247 :020 > m2 = Movie.new start_time: Time.parse("10:25"), end_time: Time.parse("10:35") | |
=> #<Movie id: nil, start_time: "2013-10-22 10:25:00", end_time: "2013-10-22 10:35:00", created_at: nil, updated_at: nil> | |
2.0.0-p247 :021 > m2.valid? | |
Movie Load (0.3ms) SELECT "movies".* FROM "movies" | |
=> true | |
2.0.0-p247 :022 > m1 = Movie.first | |
Movie Load (0.3ms) SELECT "movies".* FROM "movies" ORDER BY "movies"."id" ASC LIMIT 1 | |
=> #<Movie id: 7, start_time: "2000-01-01 08:00:00", end_time: "2000-01-01 08:30:00", created_at: "2013-10-22 14:00:43", updated_at: "2013-10-22 14:00:43"> | |
2.0.0-p247 :023 > m2.start_time - m1.end_time | |
=> 435714900.0 | |
2.0.0-p247 :024 > m1.start_time - m2.end_time | |
=> -435717300.0 | |
2.0.0-p247 :025 > m2.save | |
(0.1ms) begin transaction | |
Movie Load (0.2ms) SELECT "movies".* FROM "movies" | |
SQL (1.5ms) INSERT INTO "movies" ("created_at", "end_time", "start_time", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 22 Oct 2013 14:02:10 UTC +00:00], ["end_time", 2013-10-22 10:35:00 +0200], ["start_time", 2013-10-22 10:25:00 +0200], ["updated_at", Tue, 22 Oct 2013 14:02:10 UTC +00:00]] | |
(57.6ms) commit transaction | |
=> true | |
2.0.0-p247 :026 > m2.start_time - m1.end_time | |
=> 435714900.0 | |
2.0.0-p247 :027 > m1.reload | |
Movie Load (0.3ms) SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1 [["id", 7]] | |
=> #<Movie id: 7, start_time: "2000-01-01 08:00:00", end_time: "2000-01-01 08:30:00", created_at: "2013-10-22 14:00:43", updated_at: "2013-10-22 14:00:43"> | |
2.0.0-p247 :028 > m2.reload | |
Movie Load (0.2ms) SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1 [["id", 8]] | |
=> #<Movie id: 8, start_time: "2000-01-01 08:25:00", end_time: "2000-01-01 08:35:00", created_at: "2013-10-22 14:02:10", updated_at: "2013-10-22 14:02:10"> | |
2.0.0-p247 :029 > m2.start_time - m1.end_time | |
=> -300.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upload | |
Movie.delete_all | |
xml = params[:upload] | |
xml = Nokogiri::XML(xml){ |cfg| cfg.noblanks } # parse xml ignoring whitespace | |
xml.xpath('//movie').each do | movie | | |
Movie.from_xml(movie).save | |
end | |
redirect_to timetable_path | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Movie < ActiveRecord::Base | |
belongs_to :channel | |
validate :does_not_overlap, :valid_times | |
def self.from_xml(xml) | |
id = xml.attr('id') #parse movie id | |
channel = Channel.find_by_codename(xml.child.name) #get the channel from db | |
name = xml.child.xpath('name').text #get name | |
start_time = parse_time(xml.child.xpath('start_time').text) | |
end_time = parse_time(xml.child.xpath('end_time').text) | |
new ( {id: id, channel: channel, name: name, start_time: start_time, end_time: end_time, recording: false }) | |
end | |
def valid_times | |
if start_time >= end_time | |
errors.add(:end_time, "Movie with id #{id} end_time must be posterior to start_time") | |
end | |
end | |
def does_not_overlap | |
channel.movies.each do |movie| | |
#this trick covers both partial overlaps AND complete enclosings | |
if (start_time - movie.end_time) * (movie.start_time - end_time) >= 0 | |
errors.add(:end_time, "Movie with id #{id} overlaps with movie with id #{movie.id}") | |
end | |
end | |
end | |
private | |
def self.parse_time time | |
time.gsub!(/\./,':') #replace dot separators by colon as required by Time.parse | |
Time.parse(time) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<movie_data> | |
<movie id="1"> | |
<first_channel> | |
<name>Movie1</name> | |
<start_time>9.00am</start_time> | |
<end_time>10.05am</end_time> | |
</first_channel> | |
</movie> | |
<movie id="3"> | |
<first_channel> | |
<name>Movie2</name> | |
<start_time>10.00am</start_time> | |
<end_time>11.30am</end_time> | |
</first_channel> | |
</movie> | |
</movie_data> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment