Sha256: 98e97aa06ccbc50d1a87de3cd378273f651f8d49ec495669f2882e04214ebb00

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require 'csv'
require_relative '../../models/entry_group'
require_relative '../../support/fileable'

module Dsu
  module Services
    module EntryGroup
      class ExporterService
        include Support::Fileable

        def initialize(entry_groups:, options: {})
          raise ArgumentError, 'Argument entry_groups is nil' if entry_groups.nil?

          @entry_groups = entry_groups
          @options = options
        end

        def call
          CSV.open(export_file_name, 'w') do |csv|
            csv << %i[version entry_group entry_no total_entries entry_group_entry]

            entry_groups.each do |entry_group|
              next unless entry_group.exist?

              entry_group.entries.each_with_index do |entry, index|
                csv << [
                  entry_group.version,
                  entry_group.time.to_date,
                  index + 1,
                  entry_group.entries.count,
                  entry.description
                ]
              end
            end
          end

          export_file_name
        end

        def export_file_name
          @export_file_name ||= begin
            file_name = "dsu-#{export_timestamp}-#{times.min.to_date}-thru-#{times.max.to_date}.csv"
            File.join(temp_folder, file_name)
          end
        end

        private

        attr_reader :entry_groups, :options

        def times
          @times ||= entry_groups.map(&:time)
        end

        def export_timestamp
          Time.now.in_time_zone.strftime('%Y%m%d%H%M%S')
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dsu-2.3.1 lib/dsu/services/entry_group/exporter_service.rb
dsu-2.3.0 lib/dsu/services/entry_group/exporter_service.rb