Sha256: 2142ff30cdbacb3cb627d26c7bdf7d89921d7b1ecfccecab164ee3a5cfff49fe

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

# title: CSV Export
# description: Export CSV formatted data with header row
# author: Brett Terpstra
# url: https://brettterpstra.com
module Doing
  ##
  ## @brief      CSV Export
  ##
  class CSVExport
    include Doing::Util

    def self.settings
      {
        trigger: 'csv'
      }
    end

    def self.render(wwid, items, variables: {})
      return if items.nil?

      opt = variables[:options]

      output = [CSV.generate_line(%w[start end title note timer section])]
      items.each do |i|
        note = format_note(i.note)
        end_date = i.end_date
        interval = end_date && opt[:times] ? wwid.get_interval(i, formatted: false) : 0
        output.push(CSV.generate_line([i.date, end_date, i.title, note, interval, i.section]))
      end
      Doing.logger.debug('CSV Export:', "#{items.count} items output to CSV")
      output.join('')
    end

    def self.format_note(note)
      out = ''
      if note
        arr = note.map(&:strip).delete_if { |e| e =~ /^\s*$/ }
        out = arr.join("\n") unless arr.empty?
      end

      out
    end

    Doing::Plugins.register 'csv', :export, self
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
doing-2.0.9.pre lib/doing/plugins/export/csv_export.rb
doing-2.0.8.pre lib/doing/plugins/export/csv_export.rb
doing-2.0.7.pre lib/doing/plugins/export/csv_export.rb
doing-2.0.6.pre lib/doing/plugins/export/csv_export.rb
doing-2.0.5.pre lib/doing/plugins/export/csv_export.rb
doing-2.0.3.pre lib/doing/plugins/export/csv_export.rb
doing-2.0.2.pre lib/doing/plugins/export/csv_export.rb