Class: Smuggle::Services::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/smuggle/services/export.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver: Smuggle::Exporter::Resolver.new) ⇒ Export

Returns a new instance of Export.



10
11
12
# File 'lib/smuggle/services/export.rb', line 10

def initialize(resolver: Smuggle::Exporter::Resolver.new)
  @resolver = resolver
end

Class Method Details

.call(**args) ⇒ Object



6
7
8
# File 'lib/smuggle/services/export.rb', line 6

def self.call(**args)
  new.call(args)
end

Instance Method Details

#call(scope:, **options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/smuggle/services/export.rb', line 14

def call(scope:, **options)
  exporter = options.fetch(:exporter) { @resolver.call(scope: scope) }

  CSV.generate do |csv|
    csv << exporter.header

    method = :each
    method = :find_each if scope.respond_to?(:find_each)

    scope.public_send(method) do |record|
      csv << exporter.new(record).to_csv
    end
  end
end