Class: Tahweel::Writer
- Inherits:
-
Object
- Object
- Tahweel::Writer
- 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
-
.write(texts, base_path, formats: [:txt], **options) ⇒ void
Convenience method to write texts to files in the specified formats.
Instance Method Summary collapse
-
#extension ⇒ String
Delegates the extension retrieval to the specific writer strategy.
-
#initialize(format: :txt) ⇒ Writer
constructor
Initializes the Writer with a specific format strategy.
-
#write(texts, base_path, **options) ⇒ Object
Writes the texts to the destination using the selected strategy.
Constructor Details
#initialize(format: :txt) ⇒ Writer
Initializes the Writer with a specific format strategy.
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.
19 20 21 |
# File 'lib/tahweel/writer.rb', line 19 def self.write(texts, base_path, formats: [:txt], **) formats.each { new(format: _1).write(texts, base_path, **) } end |
Instance Method Details
#extension ⇒ String
Delegates the extension retrieval to the specific writer strategy.
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.
42 |
# File 'lib/tahweel/writer.rb', line 42 def write(texts, base_path, **) = @writer.write(texts, "#{base_path}.#{extension}", ) |