Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Commit

Permalink
Add identity fields for stop area operator, tweak operators parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
crowbot committed May 10, 2012
1 parent a9fcb21 commit 0befd50
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/models/stop_area_operator.rb
Expand Up @@ -2,7 +2,9 @@ class StopAreaOperator < ActiveRecord::Base
# This model is part of the transport data that is versioned by data generations. # This model is part of the transport data that is versioned by data generations.
# This means they have a default scope of models valid in the current data generation. # This means they have a default scope of models valid in the current data generation.
# See lib/fixmytransport/data_generation # See lib/fixmytransport/data_generation
exists_in_data_generation() exists_in_data_generation(:identity_fields => [:stop_area_id, :operator_id],
:new_record_fields => [],
:update_fields => [])
belongs_to :operator belongs_to :operator
belongs_to :stop_area belongs_to :stop_area
# virtual attribute used for adding new stop area operators # virtual attribute used for adding new stop area operators
Expand Down
17 changes: 9 additions & 8 deletions lib/parsers/operators_parser.rb
@@ -1,10 +1,10 @@
# Parser for loading operator data for stops by CRS code # Parser for loading operator data for stops by CRS code


class Parsers::OperatorsParser class Parsers::OperatorsParser


def initialize def initialize
end end

# Loads data from a file with tab-separated columns for CRS code, name, an unused field, and operator name # Loads data from a file with tab-separated columns for CRS code, name, an unused field, and operator name
# Loads name mappings from a file with tab-separated columns for alternative versions of names # Loads name mappings from a file with tab-separated columns for alternative versions of names
def parse_stop_area_operators(filepath, mapping_file="data/operators/operator_mappings.txt", stop_area_type='GRLS') def parse_stop_area_operators(filepath, mapping_file="data/operators/operator_mappings.txt", stop_area_type='GRLS')
Expand All @@ -25,20 +25,21 @@ def parse_stop_area_operators(filepath, mapping_file="data/operators/operator_ma
puts "*** Couldn't find stop for #{name} #{crs_code} #{operator_name}***" puts "*** Couldn't find stop for #{name} #{crs_code} #{operator_name}***"
next next
end end
stop_area = stop.root_stop_area(stop_area_type)
if ! stop_area
puts "No stop_area for stop #{stop.name} #{operator_name}"
next
end
operators = Operator.find(:all, :conditions => ['LOWER(name) = ?', operator_name.downcase]) operators = Operator.find(:all, :conditions => ['LOWER(name) = ?', operator_name.downcase])
if operators.empty? and operator_mappings[operator_name] if operators.empty? and operator_mappings[operator_name]
operator_name = operator_mappings[operator_name] operator_name = operator_mappings[operator_name]
# puts "using mapping for #{operator_name}" # puts "using mapping for #{operator_name}"
operators = Operator.find(:all, :conditions => ['LOWER(name) = ?', operator_name.downcase]) operators = Operator.find(:all, :conditions => ['LOWER(name) = ?', operator_name.downcase])
end end
raise "No operator #{operator_name}" if operators.empty? raise "No operator #{operator_name} for #{stop_area.name}" if operators.empty?
raise "Multiple operators #{operator_name}" if operators.size > 1 raise "Multiple operators #{operator_name}" if operators.size > 1
operator = operators.first operator = operators.first
stop_area = stop.root_stop_area(stop_area_type)
if ! stop_area
puts "No stop_area for stop #{stop.name} #{operator_name}"
next
end
stop_area_operator = stop_area.stop_area_operators.build(:operator => operator) stop_area_operator = stop_area.stop_area_operators.build(:operator => operator)
yield stop_area_operator yield stop_area_operator
# puts "#{stop_area.name} #{operator.name}" # puts "#{stop_area.name} #{operator.name}"
Expand Down

0 comments on commit 0befd50

Please sign in to comment.