Class: Pdfh::RenameValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfh/utils/rename_validator.rb

Overview

Validates the rename template, and generate new name

Constant Summary collapse

RENAME_TYPES =
{
  "original" => "No name change",
  "period"   => "Year-Month",
  "year"     => "Year",
  "month"    => "Month",
  "type"     => "Document Type name",
  "subtype"  => "Document Subtype name",
  "extra"    => "Extra data extracted from date_re"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ self

Parameters:



20
21
22
23
24
25
# File 'lib/pdfh/utils/rename_validator.rb', line 20

def initialize(template)
  @template = template
  @all = template.scan(/{([^}]+)}/).flatten.map(&:downcase)
  @unknown = all - types
  @valid = all - unknown
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



16
17
18
# File 'lib/pdfh/utils/rename_validator.rb', line 16

def all
  @all
end

#templateObject (readonly)

Returns the value of attribute template.



16
17
18
# File 'lib/pdfh/utils/rename_validator.rb', line 16

def template
  @template
end

#unknownObject (readonly)

Returns the value of attribute unknown.



16
17
18
# File 'lib/pdfh/utils/rename_validator.rb', line 16

def unknown
  @unknown
end

#validObject (readonly)

Returns the value of attribute valid.



16
17
18
# File 'lib/pdfh/utils/rename_validator.rb', line 16

def valid
  @valid
end

Instance Method Details

#gsub(values) ⇒ String (frozen)

Parameters:

  • values (Hash{Symbol->String)

    ]

Returns:



44
45
46
47
48
# File 'lib/pdfh/utils/rename_validator.rb', line 44

def gsub(values)
  template
    .gsub(/\{([^}]+)}/, &:downcase) # convert all text between {} to lowercase
    .gsub("{", "%{") % values
end

#typesArray<String>

Returns:



28
29
30
# File 'lib/pdfh/utils/rename_validator.rb', line 28

def types
  RENAME_TYPES.keys
end

#unknown_listString

Returns:



38
39
40
# File 'lib/pdfh/utils/rename_validator.rb', line 38

def unknown_list
  unknown.join(", ")
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pdfh/utils/rename_validator.rb', line 33

def valid?
  unknown.empty?
end