Class: Pdfh::DocumentType

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

Overview

Represents a type of document that can be processed by pdfh

Constant Summary collapse

REQUIRED_KEYS =
%i[name re_date store_path].freeze
DEFAULT_NAME_TEMPLATE =
"{name} {period}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ DocumentType

Parameters:

  • args (Hash)

    The initialization arguments



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

def initialize(args)
  args.each { |k, v| instance_variable_set(:"@#{k}", v) }
  return if missing_keys?

  @name = name.to_s.strip
  @re_id = Regexp.new(re_id || name)
  @re_date = Regexp.new(re_date)
  @name_template = name_template || DEFAULT_NAME_TEMPLATE
  @path_validator = RenameValidator.new(store_path)
  @name_validator = RenameValidator.new(@name_template)
end

Instance Attribute Details

#nameString (readonly)

Returns The name of the document type.

Returns:

  • (String)

    The name of the document type



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

def name
  @name
end

#name_templateString (readonly)

Returns The template for generating document names.

Returns:

  • (String)

    The template for generating document names



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

attr_reader :name, :re_id, :re_date, :store_path, :name_template, :path_validator, :name_validator

#name_validatorObject (readonly)

Returns the value of attribute name_validator.



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

attr_reader :name, :re_id, :re_date, :store_path, :name_template, :path_validator, :name_validator

#path_validatorRenameValidator (readonly)

Returns The validator for the storage path.

Returns:



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

attr_reader :name, :re_id, :re_date, :store_path, :name_template, :path_validator, :name_validator

#re_dateRegexp (readonly)

Returns The regular expression to extract dates.

Returns:

  • (Regexp)

    The regular expression to extract dates



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

attr_reader :name, :re_id, :re_date, :store_path, :name_template, :path_validator, :name_validator

#re_idRegexp (readonly)

Returns The regular expression to extract the document ID.

Returns:

  • (Regexp)

    The regular expression to extract the document ID



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

attr_reader :name, :re_id, :re_date, :store_path, :name_template, :path_validator, :name_validator

#store_pathString (readonly)

Returns The path where the document will be stored.

Returns:

  • (String)

    The path where the document will be stored



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

attr_reader :name, :re_id, :re_date, :store_path, :name_template, :path_validator, :name_validator

Instance Method Details

#gidString

removes special characters from string and replaces spaces with dashes

Examples:

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

Returns:

  • (String)


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

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

#missing_keysArray<Symbol>

Returns:

  • (Array<Symbol>)


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

def missing_keys
  @missing_keys ||= REQUIRED_KEYS.select { |key| instance_variable_get(:"@#{key}").to_s.strip.empty? }
end

#missing_keys?Boolean

Returns:

  • (Boolean)


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

def missing_keys? = missing_keys.any?

#to_hHash{String => Object}

Returns:

  • (Hash{String => Object})


47
48
49
# File 'lib/pdfh/models/document_type.rb', line 47

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  missing_keys.empty? &&
    @path_validator.valid? &&
    @name_validator.valid?
end