-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
homework #105
base: master
Are you sure you want to change the base?
homework #105
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class BusesService < ApplicationRecord | ||
belongs_to :bus | ||
belongs_to :service | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'oj' | ||
class ReloadJson | ||
def initialize(file_name) | ||
@file_name = file_name | ||
@cities = {} | ||
@buses = {} | ||
@buses_services = [] | ||
end | ||
|
||
def call | ||
ActiveRecord::Base.transaction do | ||
clear_tables | ||
create_services | ||
|
||
@connection = ActiveRecord::Base.connection.raw_connection | ||
trips_command = | ||
"copy trips (from_id, to_id, start_time, duration_minutes, price_cents, bus_id) from stdin with csv delimiter ';'" | ||
|
||
@connection.copy_data trips_command do | ||
File.open(file_name) do |ff| | ||
nesting = 0 | ||
str = String.new | ||
|
||
until ff.eof? | ||
ch = ff.read(1) # читаем по одному символу | ||
if ch == '{' # начинается объект, повышается вложенность | ||
nesting += 1 | ||
str << ch | ||
elsif ch == '}' # заканчивается объект, понижается вложенность | ||
nesting -= 1 | ||
str << ch | ||
if nesting.zero? # если закончился объкет уровня trip, парсим и импортируем его | ||
trip = Oj.load(str) | ||
copy_trip(trip) | ||
|
||
str = String.new | ||
end | ||
elsif nesting >= 1 | ||
str << ch | ||
end | ||
end | ||
end | ||
end | ||
|
||
Bus.import buses.values | ||
City.import cities.values | ||
BusesService.import buses_services | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :cities, :buses, :buses_services, :services, :file_name | ||
|
||
def create_services | ||
services = [] | ||
Service::SERVICES.each do |name| | ||
services << Service.new(name: name) | ||
end | ||
Service.import services | ||
@services = Service.all.to_h { |service| [service.name, service.id] } | ||
end | ||
|
||
def clear_tables | ||
City.delete_all | ||
Bus.delete_all | ||
Service.delete_all | ||
Trip.delete_all | ||
BusesService.delete_all | ||
end | ||
|
||
def copy_trip(trip) | ||
from_id = cities.dig(trip['from'], 'id') | ||
to_id = cities.dig(trip['to'], 'id') | ||
service_ids = services.values_at(*trip['bus']['services']) | ||
bus_key = "#{trip['bus']['model']} #{trip['bus']['number']}" | ||
bus_id = buses.dig(bus_key, 'id') | ||
|
||
unless from_id | ||
id = cities.size + 1 | ||
cities[trip['from']] = { 'id' => id, 'name' => trip['from'] } | ||
from_id = id | ||
end | ||
|
||
unless to_id | ||
if cities[trip['from']] == @cities[trip['to']] | ||
to_id = from_id | ||
else | ||
id = cities.size + 1 | ||
@cities[trip['to']] = { 'id' => id, 'name' => trip['to'] } | ||
to_id = id | ||
end | ||
end | ||
|
||
unless bus_id | ||
id = buses.size + 1 | ||
buses[bus_key] = | ||
{ 'id' => id, 'number' => trip['bus']['number'], 'model' => trip['bus']['model'] } | ||
service_ids.each { |service_id| buses_services << { 'service_id' => service_id, 'bus_id' => id } } | ||
bus_id = id | ||
end | ||
|
||
@connection.put_copy_data("#{from_id};#{to_id};#{trip['start_time']};#{trip['duration_minutes']};#{trip['price_cents']};#{bus_id}\n") | ||
end | ||
end |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
<li><%= "Отправление: #{trip.start_time}" %></li> | ||
<li><%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %></li> | ||
<li><%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %></li> | ||
<li><%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %></li> | ||
<li><%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %></li> | ||
<ul> | ||
<li><%= "Отправление: #{trip.start_time}" %></li> | ||
<li><%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %></li> | ||
<li><%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %></li> | ||
<li><%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %></li> | ||
<li><%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %></li> | ||
<% if trip.bus.services.present? %> | ||
<li>Сервисы в автобусе:</li> | ||
<ul> | ||
<% trip.bus.services.each do |service| %> | ||
<li><%= "#{service.name}" %></li> | ||
<% end %> | ||
</ul> | ||
<% end %> | ||
</ul> | ||
==================================================== |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,7 @@ | |
<%= "Автобусы #{@from.name} – #{@to.name}" %> | ||
</h1> | ||
<h2> | ||
<%= "В расписании #{@trips.count} рейсов" %> | ||
<%= "В расписании #{@trips.size} рейсов" %> | ||
</h2> | ||
|
||
<% @trips.each do |trip| %> | ||
<ul> | ||
<%= render "trip", trip: trip %> | ||
<% if trip.bus.services.present? %> | ||
<%= render "services", services: trip.bus.services %> | ||
<% end %> | ||
</ul> | ||
<%= render "delimiter" %> | ||
<% end %> | ||
<%= render partial: 'trip', collection: @trips, as: :trip %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Есть ещё забавная возможность задать разделитель параметром: https://guides.rubyonrails.org/layouts_and_rendering.html#spacer-templates |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.