# Use case to export a register into a CSV class Eco::API::UseCases::OozeCases::ExportRegisterCase < Eco::API::UseCases::OozeSamples::RegisterExportCase name "export-register" type :other batch_size 5 def main(session, options, usecase) super(session, options, usecase) do # Save the File CSV.open(filename, "w") do |csv| csv << exportable_register.header(refresh: true) exportable_register.each(as_values: true) do |values| csv << values end end end end def process_ooze(ooz) exportable_register.add_ooze(ooz) exportable_register.header end def filters [].tap do |fltrs| %i[any all].each {|mode| fltrs << build_tags_filter(mode)} %i[created_at updated_at].each {|key| fltrs << build_range_filter(key)} end.compact end private def exportable_register @exportable_register ||= exportable_register_class.new(**export_options) end def exportable_register_class Eco::API::UseCases::OozeSamples::Helpers::ExportableRegister end def build_tags_filter(mode = :any) tags = option_tags(mode) return nil if !tags || tags.empty? tags_filter(tags, any: mode == :any) end def build_range_filter(key = :update_at) from = from_date(key) to = to_date(key) return nil unless from || to date_range_filter(from: from, to: to, key: key) end def from_date(key) options.dig(:export, :options, :filters, key, :from) end def to_date(key) options.dig(:export, :options, :filters, key, :to) end def option_tags(mode = :any) options.dig(:export, :options, :filters, :tags, mode) end def export_options @export_options ||= { delimiter: options_delimiter, only_indexed: include_deindexed?, only_labeled: include_unnamed?, only_with_ref: include_unhashed? } end def include_unnamed? options.dig(:export, :options, :include, :unnamed) end def options_delimiter options.dig(:export, :options, :delimiter) || "|" end def include_deindexed? options.dig(:export, :options, :include, :deindexed) end def include_unhashed? options.dig(:export, :options, :include, :unhashed) end def filename @filename ||= (options[:file] || options.dig(:export, :file, :name)).tap do |filename| unless filename log(:error) { "Destination file not specified" } return false end end end end