module Wpxf::Helpers::Export

Provides helper methods for common functionality used to export files.

Public Instance Methods

export_and_log_loot(content, description, type, extension = '') click to toggle source

Save content to a new file and log the loot in the database. @param content [String] the file content to save. @param extension [String] the file extension to use when creating the file. @return [Models::LootItem] the newly created {Models::LootItem}.

# File lib/wpxf/helpers/export.rb, line 41
def export_and_log_loot(content, description, type, extension = '')
  filename = generate_unique_filename(extension)
  File.write(filename, content)
  store_loot filename, description, type
end
export_path() click to toggle source

@return [String] the path to save the file to.

# File lib/wpxf/helpers/export.rb, line 24
def export_path
  return nil if normalized_option_value('export_path').nil?
  File.expand_path normalized_option_value('export_path')
end
generate_unique_filename(file_extension) click to toggle source

@return [String] the path to a unique filename in the wpxf home directory.

# File lib/wpxf/helpers/export.rb, line 30
def generate_unique_filename(file_extension)
  storage_path = File.join(Dir.home, '.wpxf', 'loot')
  FileUtils.mkdir_p(storage_path) unless File.directory?(storage_path)
  filename = "#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}#{file_extension}"
  File.join(storage_path, filename)
end
register_export_path_option(required) click to toggle source

Register the export_path option. @param required [Boolean] a value indicating whether the option should be required. @return [StringOption] the newly registered option.

# File lib/wpxf/helpers/export.rb, line 12
def register_export_path_option(required)
  opt = StringOption.new(
    name: 'export_path',
    desc: 'The path to save the file to',
    required: required
  )

  register_option(opt)
  opt
end