Class: Pdfh::DocumentType

Inherits:
Object
  • Object
show all
Includes:
Concerns::PasswordDecodable
Defined in:
lib/pdfh/models/document_type.rb

Overview

Represents a type of document that can be processed by pdfh

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::PasswordDecodable

#password, #password?

Constructor Details

#initialize(args) ⇒ self

Parameters:

  • args (Hash)


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

def initialize(args)
  args.each { |k, v| instance_variable_set(:"@#{k}", v) }
  @name_template ||= "{original}"
  @re_file = Regexp.new(re_file)
  @re_date = Regexp.new(re_date)
  @sub_types = extract_subtypes(sub_types) if sub_types&.any?
  @path_validator = RenameValidator.new(store_path)
  @name_validator = RenameValidator.new(name_template)
  return if @path_validator.valid? && @name_validator.valid?

  raise_validators_error
end

Instance Attribute Details

#nameString (readonly)

Returns The name of the document type.

Returns:

  • (String)

    The name of the document type.



22
23
24
# File 'lib/pdfh/models/document_type.rb', line 22

def name
  @name
end

#name_templateString (readonly)

Returns The template for generating document names.

Returns:

  • (String)

    The template for generating document names.



22
# File 'lib/pdfh/models/document_type.rb', line 22

attr_reader :name, :re_file, :re_date, :pwd, :store_path, :name_template, :sub_types

#pwdString? (readonly)

Returns The base64 password for the document type, if any.

Returns:

  • (String, nil)

    The base64 password for the document type, if any.



22
# File 'lib/pdfh/models/document_type.rb', line 22

attr_reader :name, :re_file, :re_date, :pwd, :store_path, :name_template, :sub_types

#re_dateRegexp (readonly)

Returns The regular expression to extract dates and its information.

Returns:

  • (Regexp)

    The regular expression to extract dates and its information.



22
# File 'lib/pdfh/models/document_type.rb', line 22

attr_reader :name, :re_file, :re_date, :pwd, :store_path, :name_template, :sub_types

#re_fileRegexp (readonly)

Returns The regular expression to match file names.

Returns:

  • (Regexp)

    The regular expression to match file names.



22
# File 'lib/pdfh/models/document_type.rb', line 22

attr_reader :name, :re_file, :re_date, :pwd, :store_path, :name_template, :sub_types

#store_pathString (readonly)

Returns The path where the document will be stored.

Returns:

  • (String)

    The path where the document will be stored.



22
# File 'lib/pdfh/models/document_type.rb', line 22

attr_reader :name, :re_file, :re_date, :pwd, :store_path, :name_template, :sub_types

#sub_typesObject (readonly)

Returns the value of attribute sub_types.



22
# File 'lib/pdfh/models/document_type.rb', line 22

attr_reader :name, :re_file, :re_date, :pwd, :store_path, :name_template, :sub_types

Instance Method Details

#generate_new_name(values) ⇒ String

Parameters:

  • values (Hash{Symbol->String)

Returns:



62
63
64
# File 'lib/pdfh/models/document_type.rb', line 62

def generate_new_name(values)
  @name_validator.gsub(values)
end

#generate_path(values) ⇒ String

Parameters:

  • values (Hash{Symbol->String)

Returns:



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

def generate_path(values)
  @path_validator.gsub(values)
end

#gidString

removes special characters from string and replaces spaces with dashes

Examples:

usage

"Test This?%&".gid
# => "test-this"

Returns:



49
50
51
# File 'lib/pdfh/models/document_type.rb', line 49

def gid
  name.downcase.gsub(/[^0-9A-Za-z\s]/, "").tr(" ", "-")
end

#sub_type(text) ⇒ DocumentSubType

search the subtype name in the pdf document

Returns:



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

def sub_type(text)
  # Regexp.new(st.name).match?(name)
  sub_types&.find { |st| /#{st.name}/i.match?(text) }
end

#to_hHash{Symbol->any

Returns ].

Returns:

  • (Hash{Symbol->any)

    ]



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

def to_h
  instance_variables.to_h { |var| [var.to_s.delete_prefix("@"), instance_variable_get(var)] }
end