Class: Pdfh::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfh/models/document.rb

Overview

Lightweight struct that connects a PDF file with its matched document type and extracted text. All file metadata, date interpretation, and rename resolution are accessible through dedicated value objects (FileInfo, DateInfo).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, type, text, date_captures) ⇒ self

Returns A new Document instance.

Parameters:

  • file (String)

    Path to the PDF file

  • type (DocumentType)

    Type of the document

  • text (String)

    Extracted text from the PDF

  • date_captures (Hash{String => String})

    Captured date components from regex



23
24
25
26
27
28
# File 'lib/pdfh/models/document.rb', line 23

def initialize(file, type, text, date_captures)
  @type = type
  @text = text
  @file_info = FileInfo.new(file)
  @date_info = DateInfo.new(date_captures)
end

Instance Attribute Details

#date_infoObject (readonly)

Returns the value of attribute date_info.



16
# File 'lib/pdfh/models/document.rb', line 16

attr_reader :file_info, :type, :text, :date_info

#file_infoFileInfo (readonly)

Returns File metadata wrapper.

Returns:



16
17
18
# File 'lib/pdfh/models/document.rb', line 16

def file_info
  @file_info
end

#textString (readonly)

Returns Extracted text from the PDF.

Returns:

  • (String)

    Extracted text from the PDF



16
# File 'lib/pdfh/models/document.rb', line 16

attr_reader :file_info, :type, :text, :date_info

#typeDocumentType (readonly)

Returns Matched document type.

Returns:



16
# File 'lib/pdfh/models/document.rb', line 16

attr_reader :file_info, :type, :text, :date_info

Instance Method Details

#new_nameString

Returns New file name with extension (e.g., "2024-01 Cuenta.pdf").

Returns:

  • (String)

    New file name with extension (e.g., "2024-01 Cuenta.pdf")



41
42
43
# File 'lib/pdfh/models/document.rb', line 41

def new_name
  "#{@type.name_validator.gsub(rename_data)}#{@file_info.extension}"
end

#store_pathString

Returns Storage path for the document (e.g., "2024/Edo Cuenta").

Returns:

  • (String)

    Storage path for the document (e.g., "2024/Edo Cuenta")



46
47
48
# File 'lib/pdfh/models/document.rb', line 46

def store_path
  @type.path_validator.gsub(rename_data)
end

#to_sString

Returns File name.

Returns:

  • (String)

    File name



36
37
38
# File 'lib/pdfh/models/document.rb', line 36

def to_s
  file_info.name
end

#type_nameString

Returns Document type name or "N/A" if type is nil.

Returns:

  • (String)

    Document type name or "N/A" if type is nil



31
32
33
# File 'lib/pdfh/models/document.rb', line 31

def type_name
  type&.name || "N/A"
end