diff options
| author | Zog | 2018-01-30 20:49:24 +0100 | 
|---|---|---|
| committer | Johan Van Ryseghem | 2018-02-20 09:50:28 +0100 | 
| commit | beadf103e66f752706e9bf16931e80dc1629c1db (patch) | |
| tree | e4e50ed8617c919ab97230cf5429835aeeda7128 /app | |
| parent | 51e08724766bf2ca4837436178984c33d22cf16a (diff) | |
| download | chouette-core-beadf103e66f752706e9bf16931e80dc1629c1db.tar.bz2 | |
Refs #5765; Add new task to import in a given referential
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/simple_importer.rb | 59 | 
1 files changed, 43 insertions, 16 deletions
diff --git a/app/models/simple_importer.rb b/app/models/simple_importer.rb index 4f549c408..6ab013b04 100644 --- a/app/models/simple_importer.rb +++ b/app/models/simple_importer.rb @@ -22,6 +22,13 @@ class SimpleImporter < ActiveRecord::Base      self.journal ||= []    end +  def configure +    new_config = configuration.duplicate +    yield new_config +    new_config.validate! +    self.configuration = new_config +  end +    def resolve col_name, value, &block      val = block.call(value)      return val if val.present? @@ -67,7 +74,7 @@ class SimpleImporter < ActiveRecord::Base          new_record = @current_record.new_record?          @current_record.save!          self.journal.push({event: (new_record ? :creation : :update), kind: :log}) -        statuses += new_record ? "✓" : "-" +        statuses += new_record ? colorize("✓", :green) : colorize("-", :orange)        end        self.configuration.columns.each do |col|          if col.name && @resolution_queue.any? @@ -105,25 +112,26 @@ class SimpleImporter < ActiveRecord::Base      end    end +  def colorize txt, color +    color = { +      red: "31", +      green: "32", +      orange: "33", +    }[color] || "33" +    "\e[#{color}m#{txt}\e[0m" +  end +    def log msg, opts={}      return unless @verbose      out = "" -    if opts[:color] -      color = { -        red: "31", -        green: "32", -        orange: "33", -      }[opts[:color]] || "33" -    end -    out += "\e[#{color}m" if color +    msg = colorize(msg, opts[:color]) if opts[:color]      if opts[:clear] && @prev_msg_size        out += "\b"*@prev_msg_size      end      out += msg -    out += "\e[0m" if color      print out      @prev_msg_size = msg.size -    @prev_msg_size += 9 if color +    # @prev_msg_size += 9 if opts[:color]    end    class FailedImport < RuntimeError @@ -133,12 +141,27 @@ class SimpleImporter < ActiveRecord::Base      attr_accessor :model, :headers, :separator, :key      attr_reader :columns -    def initialize import_name +    def initialize import_name, opts={}        @import_name = import_name -      @key = "id" -      @headers = true -      @separator = "," -      @columns = [] +      @key = opts[:key] || "id" +      @headers = opts.has_key?(:headers) ? opts[:headers] : true +      @separator = opts[:separator] || "," +      @columns = opts[:columns] || [] +      @model = opts[:model] +    end + +    def duplicate +      Configuration.new @import_name, self.options +    end + +    def options +      { +        key: @key, +        headers: @headers, +        separator: @separator, +        columns: @columns.map(&:duplicate), +        model: model +      }      end      def validate! @@ -177,6 +200,10 @@ class SimpleImporter < ActiveRecord::Base          @options[:attribute] ||= @name        end +      def duplicate +        Column.new @options.dup +      end +        def [](key)          @options[key]        end  | 
