Sha256: 95d46ffc0fdd280e099d55ebc0cc955e7ce1dcd98e2b5f5c230cdfe6dfe3b719

Contents?: true

Size: 1.14 KB

Versions: 18

Compression:

Stored size: 1.14 KB

Contents

module CSVImporter
  # Generate a human readable message for the given report.
  class ReportMessage
    def self.call(report)
      new(report).to_s
    end

    def initialize(report)
      @report = report
    end

    attr_accessor :report

    def to_s
      send("report_#{report.status}")
    end

    private

    def report_pending
      "Import hasn't started yet"
    end

    def report_in_progress
      "Import in progress"
    end

    def report_done
      "Import completed: " + import_details
    end

    def report_invalid_header
      "The following columns are required: #{report.missing_columns.join(", ")}"
    end

    def report_invalid_csv_file
      report.parser_error
    end

    def report_aborted
      "Import aborted"
    end

    # Generate something like: "3 created. 4 updated. 1 failed to create. 2 failed to update."
    def import_details
      report.attributes
        .select { |name, _| name["_rows"] }
        .select { |_, instances| instances.size > 0 }
        .map { |bucket, instances| "#{instances.size} #{bucket.to_s.gsub('_rows', '').gsub('_', ' ')}" }
        .join(", ")
    end

  end # class ReportMessage
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/csv-importer-0.8.2/lib/csv_importer/report_message.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/csv-importer-0.8.2/lib/csv_importer/report_message.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/csv-importer-0.8.2/lib/csv_importer/report_message.rb
csv-importer-0.8.2 lib/csv_importer/report_message.rb
csv-importer-0.8.1 lib/csv_importer/report_message.rb
csv-importer-0.8.0 lib/csv_importer/report_message.rb
csv-importer-0.7.0 lib/csv_importer/report_message.rb
csv-importer-0.6.0 lib/csv_importer/report_message.rb
csv-importer-0.5.0 lib/csv_importer/report_message.rb
csv-importer-0.4.0 lib/csv_importer/report_message.rb
csv-importer-0.3.2 lib/csv_importer/report_message.rb
csv-importer-0.3.1 lib/csv_importer/report_message.rb
csv-importer-0.3.0 lib/csv_importer/report_message.rb
csv-importer-0.2.0 lib/csv_importer/report_message.rb
csv-importer-0.1.3 lib/csv_importer/report_message.rb
csv-importer-0.1.2 lib/csv_importer/report_message.rb
csv-importer-0.1.1 lib/csv_importer/report_message.rb
csv-importer-0.1.0 lib/csv_importer/report_message.rb