diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 4 | ||||
| -rw-r--r-- | app/models/simple_exporter.rb | 3 | ||||
| -rw-r--r-- | app/models/simple_interface.rb | 6 | ||||
| -rw-r--r-- | app/models/simple_json_exporter.rb | 53 |
4 files changed, 46 insertions, 20 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 49a2b387e..194dc85ff 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -156,11 +156,11 @@ module Chouette end def sales_start - purchase_windows.map{|p| p.date_ranges.first}.min + purchase_windows.map{|p| p.date_ranges.map &:first}.flatten.min end def sales_end - purchase_windows.map{|p| p.date_ranges.last}.max + purchase_windows.map{|p| p.date_ranges.map &:last}.flatten.max end def calculate_vehicle_journey_at_stop_day_offset diff --git a/app/models/simple_exporter.rb b/app/models/simple_exporter.rb index adc48533f..721e56dda 100644 --- a/app/models/simple_exporter.rb +++ b/app/models/simple_exporter.rb @@ -99,6 +99,7 @@ class SimpleExporter < SimpleInterface def handle_item item number_of_lines = @number_of_lines + @current_item = item map_item_to_rows(item).each_with_index do |item, i| @number_of_lines = number_of_lines + i @current_row = item.attributes @@ -112,7 +113,7 @@ class SimpleExporter < SimpleInterface end push_in_journal({event: :success, kind: :log}) @statuses += @new_status - print_state if @current_line % 20 == 0 + print_state if @current_line % 20 == 0 || i > 0 @current_line += 1 @csv << row end diff --git a/app/models/simple_interface.rb b/app/models/simple_interface.rb index 07fabd832..5a1c3dca8 100644 --- a/app/models/simple_interface.rb +++ b/app/models/simple_interface.rb @@ -33,7 +33,7 @@ class SimpleInterface < ActiveRecord::Base @errors = [] @messages = [] @padding = 1 - @current_line = 0 + @current_line = -1 @padding = [1, Math.log([@number_of_lines, 1].max, 10).ceil()].max end @@ -280,6 +280,10 @@ class SimpleInterface < ActiveRecord::Base !!@options[:required] end + def omit_nil? + !!@options[:omit_nil] + end + def scope @options[:scope] || [] end diff --git a/app/models/simple_json_exporter.rb b/app/models/simple_json_exporter.rb index 706307de1..44ecfcba2 100644 --- a/app/models/simple_json_exporter.rb +++ b/app/models/simple_json_exporter.rb @@ -68,7 +68,9 @@ class SimpleJsonExporter < SimpleExporter def resolve_node item, node vals = [] - [item.send(node.attribute)].flatten.each do |node_item| + scoped_item = node.scope.inject(item){|tmp, scope| tmp.send(scope)} + + [scoped_item.send(node.attribute)].flatten.each do |node_item| item_val = {} apply_configuration node_item, node.configuration, item_val vals.push item_val @@ -79,7 +81,7 @@ class SimpleJsonExporter < SimpleExporter def apply_configuration item, configuration, output configuration.columns.each do |col| val = resolve_value item, col - output[col.name] = val + output[col.name] = val unless val.nil? && col.omit_nil? end configuration.nodes.each do |node| @@ -89,20 +91,25 @@ class SimpleJsonExporter < SimpleExporter end def handle_item item - serialized_item = {} - @current_row = item.attributes - @current_row = @current_row.slice(*configuration.logged_attributes) if configuration.logged_attributes.present? - @new_status = nil + number_of_lines = @number_of_lines + @current_item = item + map_item_to_rows(item).each_with_index do |item, i| + @number_of_lines = number_of_lines + i + serialized_item = {} + @current_row = item.attributes + @current_row = @current_row.slice(*configuration.logged_attributes) if configuration.logged_attributes.present? + @new_status = nil - apply_configuration item, self.configuration, serialized_item + apply_configuration item, self.configuration, serialized_item - @new_status ||= colorize("✓", :green) + @new_status ||= colorize("✓", :green) - push_in_journal({event: :success, kind: :log}) - @statuses += @new_status - print_state if @current_line % 20 == 0 - @current_line += 1 - append_item serialized_item + push_in_journal({event: :success, kind: :log}) + @statuses += @new_status + print_state if @current_line % 20 == 0 || i > 0 + @current_line += 1 + append_item serialized_item + end end def append_item serialized_item @@ -116,13 +123,23 @@ class SimpleJsonExporter < SimpleExporter alias_method :add_field, :add_column def initialize import_name, opts={} - @nodes = [] super import_name, opts + @collection = opts[:collection] + @nodes = opts[:nodes] || [] + @root = opts[:root] + end + + def options + super.update({ + nodes: @nodes, + root: @root, + }) end def add_node name, opts={} @nodes ||= [] - node = Node.new({name: name.to_s}.update(opts)) + @scope ||= [] + node = Node.new({name: name.to_s, scope: @scope.dup}.update(opts)) yield node.configuration @nodes.push node end @@ -154,11 +171,15 @@ class SimpleJsonExporter < SimpleExporter end def attribute - name + @options[:attribute] || name end def multiple !!@options[:multiple] end + + def scope + @options[:scope] || [] + end end end |
