Class: Pdfh::DocumentType
- Inherits:
-
Object
- Object
- Pdfh::DocumentType
- 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
-
#name ⇒ String
readonly
The name of the document type.
-
#name_template ⇒ String
readonly
The template for generating document names.
-
#name_validator ⇒ Object
readonly
Returns the value of attribute name_validator.
-
#path_validator ⇒ RenameValidator
readonly
The validator for the storage path.
-
#re_date ⇒ Regexp
readonly
The regular expression to extract dates.
-
#re_id ⇒ Regexp
readonly
The regular expression to extract the document ID.
-
#store_path ⇒ String
readonly
The path where the document will be stored.
Instance Method Summary collapse
-
#gid ⇒ String
removes special characters from string and replaces spaces with dashes.
- #initialize(args) ⇒ DocumentType constructor
- #missing_keys ⇒ Array<Symbol>
- #missing_keys? ⇒ Boolean
- #to_h ⇒ Hash{String => Object}
- #valid? ⇒ Boolean
Constructor Details
#initialize(args) ⇒ DocumentType
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
#name ⇒ String (readonly)
Returns The name of the document type.
23 24 25 |
# File 'lib/pdfh/models/document_type.rb', line 23 def name @name end |
#name_template ⇒ String (readonly)
Returns 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_validator ⇒ Object (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_validator ⇒ RenameValidator (readonly)
Returns The validator for the storage path.
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_date ⇒ Regexp (readonly)
Returns 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_id ⇒ Regexp (readonly)
Returns 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_path ⇒ String (readonly)
Returns 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
#gid ⇒ String
removes special characters from string and replaces spaces with dashes
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_keys ⇒ 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
65 |
# File 'lib/pdfh/models/document_type.rb', line 65 def missing_keys? = missing_keys.any? |
#to_h ⇒ 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
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 |