aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorZog2018-02-14 12:22:30 +0100
committerJohan Van Ryseghem2018-02-20 09:50:28 +0100
commite168407ec0b842a73b42b5179936562b005d244b (patch)
treef6ae7c35fb4f7bf32c203a3b559b99b8a941a0d8 /app/models
parentee002e5aef7e5bb8b818b56ed54b6c68d074110e (diff)
downloadchouette-core-e168407ec0b842a73b42b5179936562b005d244b.tar.bz2
Refs #5924 @2h; Provide a mechanism to define a custom importer
Diffstat (limited to 'app/models')
-rw-r--r--app/models/chouette/journey_pattern.rb16
-rw-r--r--app/models/chouette/route.rb12
-rw-r--r--app/models/simple_importer.rb76
3 files changed, 83 insertions, 21 deletions
diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb
index aa9fdb810..830e985d9 100644
--- a/app/models/chouette/journey_pattern.rb
+++ b/app/models/chouette/journey_pattern.rb
@@ -170,5 +170,21 @@ module Chouette
end
full
end
+
+ def set_distances distances
+ raise "inconsistent data: #{distances.count} values for #{stop_points.count} stops" unless distances.count == stop_points.count
+ prev = distances[0].to_i
+ _costs = self.costs
+ distances[1..-1].each_with_index do |distance, i|
+ distance = distance.to_i
+ relative = distance - prev
+ prev = distance
+ start, stop = stop_points[i..i+1]
+ key = "#{start.stop_area_id}-#{stop.stop_area_id}"
+ _costs[key] ||= {}
+ _costs[key]["distance"] = relative
+ end
+ self.costs = _costs
+ end
end
end
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb
index 5cc5d8b0d..e418134de 100644
--- a/app/models/chouette/route.rb
+++ b/app/models/chouette/route.rb
@@ -185,6 +185,18 @@ module Chouette
return true
end
+ def full_journey_pattern
+ out = journey_patterns.find{|jp| jp.stop_points.count == self.stop_points.count }
+ unless out
+ out = journey_patterns.build name: self.name
+ self.stop_points.each do |sp|
+ out.stop_points.build stop_area: sp.stop_area, position: sp.position
+ end
+ out.save!
+ end
+ out
+ end
+
protected
def self.vehicle_journeys_timeless(stop_point_id)
diff --git a/app/models/simple_importer.rb b/app/models/simple_importer.rb
index 760b98610..41ce379db 100644
--- a/app/models/simple_importer.rb
+++ b/app/models/simple_importer.rb
@@ -29,6 +29,10 @@ class SimpleImporter < ActiveRecord::Base
self.configuration = new_config
end
+ def context
+ self.configuration.context || {}
+ end
+
def resolve col_name, value, &block
val = block.call(value)
return val if val.present?
@@ -54,24 +58,7 @@ class SimpleImporter < ActiveRecord::Base
self.configuration.before_actions(:all).each &:call
CSV.foreach(filepath, self.configuration.csv_options) do |row|
- @current_record = self.configuration.find_record row
- self.configuration.columns.each do |col|
- @current_attribute = col[:attribute]
- val = col[:value]
- if val.nil? || val.is_a?(Proc)
- if row.has_key? col.name
- if val.is_a?(Proc)
- val = instance_exec(row[col.name], &val)
- else
- val = row[col.name]
- end
- else
- self.journal.push({event: :column_not_found, message: "Column not found: #{col.name}", kind: :warning})
- status = :success_with_warnings
- end
- end
- @current_record.send "#{@current_attribute}=", val if val
- end
+ status = handle_row row, status
fail_with_error ->(){ @current_record.errors.messages } do
new_record = @current_record.new_record?
@@ -100,6 +87,9 @@ class SimpleImporter < ActiveRecord::Base
current_line += 1
log "#{"%#{padding}d" % current_line}/#{number_of_lines}: #{statuses}", clear: true
end
+ self.configuration.after_actions(:all).each do |action|
+ action.call self
+ end
end
self.update_attribute :status, status
rescue FailedImport
@@ -108,8 +98,6 @@ class SimpleImporter < ActiveRecord::Base
self.save!
end
- protected
-
def fail_with_error msg
begin
yield
@@ -121,6 +109,34 @@ class SimpleImporter < ActiveRecord::Base
end
end
+ protected
+
+ def handle_row row, status
+ if self.configuration.get_custom_handler
+ instance_exec(row, &self.configuration.get_custom_handler)
+ else
+ @current_record = self.configuration.find_record row
+ self.configuration.columns.each do |col|
+ @current_attribute = col[:attribute]
+ val = col[:value]
+ if val.nil? || val.is_a?(Proc)
+ if row.has_key? col.name
+ if val.is_a?(Proc)
+ val = instance_exec(row[col.name], &val)
+ else
+ val = row[col.name]
+ end
+ else
+ self.journal.push({event: :column_not_found, message: "Column not found: #{col.name}", kind: :warning})
+ status = :success_with_warnings
+ end
+ end
+ @current_record.send "#{@current_attribute}=", val if val
+ end
+ end
+ status
+ end
+
def colorize txt, color
color = {
red: "31",
@@ -146,7 +162,7 @@ class SimpleImporter < ActiveRecord::Base
end
class Configuration
- attr_accessor :model, :headers, :separator, :key
+ attr_accessor :model, :headers, :separator, :key, :context
attr_reader :columns
def initialize import_name, opts={}
@@ -205,11 +221,29 @@ class SimpleImporter < ActiveRecord::Base
@before[group].push block
end
+ def after group=:all, &block
+ @after ||= Hash.new{|h, k| h[k] = []}
+ @after[group].push block
+ end
+
def before_actions group=:all
@before ||= Hash.new{|h, k| h[k] = []}
@before[group]
end
+ def after_actions group=:all
+ @after ||= Hash.new{|h, k| h[k] = []}
+ @after[group]
+ end
+
+ def custom_handler &block
+ @custom_handler = block
+ end
+
+ def get_custom_handler
+ @custom_handler
+ end
+
class Column
attr_accessor :name
def initialize opts={}