Class: Tahweel::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/tahweel/writer.rb

Overview

Factory class for writing extracted text to different formats.

Constant Summary collapse

AVAILABLE_FORMATS =
%i[txt docx json].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format: :txt) ⇒ Writer

Initializes the Writer with a specific format strategy.

Parameters:

  • format (Symbol) (defaults to: :txt)

    The output format.

Raises:

  • (ArgumentError)

    If the format is unknown.



27
28
29
30
31
32
33
34
# File 'lib/tahweel/writer.rb', line 27

def initialize(format: :txt)
  @writer = case format
            when :txt then Writers::Txt.new
            when :docx then Writers::Docx.new
            when :json then Writers::Json.new
            else raise ArgumentError, "Unknown format: #{format}"
            end
end

Class Method Details

.write(texts, base_path, formats: [:txt], **options) ⇒ void

This method returns an undefined value.

Convenience method to write texts to files in the specified formats.

Parameters:

  • texts (Array<String>)

    The extracted texts.

  • base_path (String)

    The base output path (without extension).

  • formats (Array<Symbol>) (defaults to: [:txt])

    The output formats (default: [:txt]).

  • options (Hash)

    Options for writers.



19
20
21
# File 'lib/tahweel/writer.rb', line 19

def self.write(texts, base_path, formats: [:txt], **options)
  formats.each { new(format: _1).write(texts, base_path, **options) }
end

Instance Method Details

#extensionString

Delegates the extension retrieval to the specific writer strategy.

Returns:

  • (String)

    The file extension.



47
# File 'lib/tahweel/writer.rb', line 47

def extension = @writer.extension

#write(texts, base_path, **options) ⇒ Object

Writes the texts to the destination using the selected strategy. Appends the appropriate extension to the base path.

Parameters:

  • texts (Array<String>)

    The extracted texts.

  • base_path (String)

    The base output file path.

  • options (Hash)

    Options to pass to the writer.



42
# File 'lib/tahweel/writer.rb', line 42

def write(texts, base_path, **options) = @writer.write(texts, "#{base_path}.#{extension}", options)