module ActiveModel class ArrayExporter attr_reader :collection, :exporter, :scope def initialize(collection, options = {}) @collection = collection @scope = options.delete(:scope) @exporter = options.delete(:exporter) end def to_csv generate_file end def to_xls generate_file(col_sep: "\t") end def generate_file(options = {}) CSV.generate(options) do |file| collection.each do |object| file << exporter_for(object).values end end end private def exporter_for(object) exporter_class = exporter || Exporter.exporter_for(object) exporter_class.new(object, scope: scope) end end end