Class: Pdfh::Document

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

Overview

Handles the PDF detected by the rules

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, type, text) ⇒ self

Parameters:



12
13
14
15
16
# File 'lib/pdfh/models/document.rb', line 12

def initialize(file, type, text)
  @file = file
  @type = type
  @text = text
end

Instance Attribute Details

#extraObject (readonly)

Returns the value of attribute extra.



6
7
8
# File 'lib/pdfh/models/document.rb', line 6

def extra
  @extra
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/pdfh/models/document.rb', line 6

def file
  @file
end

#periodObject (readonly)

Returns the value of attribute period.



6
7
8
# File 'lib/pdfh/models/document.rb', line 6

def period
  @period
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/pdfh/models/document.rb', line 6

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/pdfh/models/document.rb', line 6

def type
  @type
end

Instance Method Details

#backup_nameString

Returns:



63
64
65
# File 'lib/pdfh/models/document.rb', line 63

def backup_name
  "#{file_name}.bkp"
end

#companion_files(join: false) ⇒ String (frozen)

Returns:



102
103
104
105
106
# File 'lib/pdfh/models/document.rb', line 102

def companion_files(join: false)
  return @companion unless join

  @companion.empty? ? "N/A" : @companion.join(", ")
end

#file_extensionString

Returns:



53
54
55
# File 'lib/pdfh/models/document.rb', line 53

def file_extension
  File.extname(@file)
end

#file_nameString

Returns:



58
59
60
# File 'lib/pdfh/models/document.rb', line 58

def file_name
  File.basename(@file)
end

#file_name_onlyString

Returns:



48
49
50
# File 'lib/pdfh/models/document.rb', line 48

def file_name_only
  File.basename(@file, file_extension)
end

#home_dirString

Returns:



109
110
111
# File 'lib/pdfh/models/document.rb', line 109

def home_dir
  File.dirname(@file)
end

#new_nameString

Returns:



91
92
93
94
# File 'lib/pdfh/models/document.rb', line 91

def new_name
  new_name = type.generate_new_name(rename_data)
  "#{new_name}#{file_extension}"
end

This method returns an undefined value.



32
33
34
35
36
37
38
39
40
# File 'lib/pdfh/models/document.rb', line 32

def print_info
  print_info_line "Type", type.name
  print_info_line "Sub-Type", sub_type
  print_info_line "Period", period
  print_info_line "New Name", new_name
  print_info_line "Store Path", store_path
  print_info_line "Extra files", companion_files(join: true)
  print_info_line "Processed?", "No (in Dry mode)" if Pdfh.dry?
end

This method returns an undefined value.



43
44
45
# File 'lib/pdfh/models/document.rb', line 43

def print_info_line(property, info)
  Pdfh.ident_print property, info.to_s, color: :light_blue, width: 12
end

#processvoid

This method returns an undefined value.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pdfh/models/document.rb', line 19

def process
  Pdfh.debug "=== Document Type: #{type.name} =============================="
  Pdfh.debug "~~~~~~~~~~~~~~~~~~ Finding a subtype"
  @sub_type = type.sub_type(@text)
  Pdfh.debug "  SubType: #{@sub_type}"
  @companion = search_companion_files

  month, year, @extra = match_date(@sub_type&.re_date || @type.re_date)
  @period = DocumentPeriod.new(day: extra, month: month, month_offset: @sub_type&.month_offset, year: year)
  Pdfh.debug "  Period: #{@period.inspect}"
end

#rename_dataHash{Symbol->String

Returns ].

Returns:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdfh/models/document.rb', line 78

def rename_data
  {
    original: file_name_only,
    period: period.to_s,
    year: period.year.to_s,
    month: period.month.to_s,
    type: type_name,
    subtype: sub_type,
    extra: extra || ""
  }.freeze
end

#store_pathString

Returns:



97
98
99
# File 'lib/pdfh/models/document.rb', line 97

def store_path
  type.generate_path(rename_data)
end

#sub_typeString

Returns:



73
74
75
# File 'lib/pdfh/models/document.rb', line 73

def sub_type
  @sub_type&.name&.titleize || "N/A"
end

#to_sString

Returns:



114
115
116
# File 'lib/pdfh/models/document.rb', line 114

def to_s
  @file
end

#type_nameString

Returns:



68
69
70
# File 'lib/pdfh/models/document.rb', line 68

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