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
17
18
19
20
21
22
23
24
25
# File 'lib/pdfh/models/document.rb', line 12

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

  month, year, @extra = match_data
  @period = DocumentPeriod.new(day: extra, month: month, month_offset: @sub_type&.month_offset, year: year)
  Pdfh.debug "  Period: #{@period.inspect}"
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:



60
61
62
# File 'lib/pdfh/models/document.rb', line 60

def backup_name
  "#{file_name}.bkp"
end

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

Returns:



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

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

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

#file_extensionString

Returns:



50
51
52
# File 'lib/pdfh/models/document.rb', line 50

def file_extension
  File.extname(@file)
end

#file_nameString

Returns:



55
56
57
# File 'lib/pdfh/models/document.rb', line 55

def file_name
  File.basename(@file)
end

#file_name_onlyString

Returns:



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

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

#home_dirString

Returns:



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

def home_dir
  File.dirname(@file)
end

#new_nameString

Returns:



88
89
90
91
# File 'lib/pdfh/models/document.rb', line 88

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

Returns:



99
100
101
102
103
104
# File 'lib/pdfh/models/document.rb', line 99

def print_cmd
  return "N/A" if type.print_cmd.nil? || type.print_cmd.empty?

  relative_path = File.join(store_path, new_name)
  "#{type.print_cmd} #{relative_path}"
end

This method returns an undefined value.



28
29
30
31
32
33
34
35
36
37
# File 'lib/pdfh/models/document.rb', line 28

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 "Print CMD", print_cmd
  print_info_line "Processed?", "No (in Dry mode)" if Pdfh.dry?
end

This method returns an undefined value.



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

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

#rename_dataHash{Symbol->String

Returns ].

Returns:



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pdfh/models/document.rb', line 75

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:



94
95
96
# File 'lib/pdfh/models/document.rb', line 94

def store_path
  type.generate_path(rename_data)
end

#sub_typeString

Returns:



70
71
72
# File 'lib/pdfh/models/document.rb', line 70

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

#to_sString

Returns:



119
120
121
# File 'lib/pdfh/models/document.rb', line 119

def to_s
  @file
end

#type_nameString

Returns:



65
66
67
# File 'lib/pdfh/models/document.rb', line 65

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