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",
  "quarter"  => "Quarter (Q1-Q4)",
  "bimester" => "Bimester (B1-B6)",
  "name"     => "Document Type name",
  "day"      => "Day of month"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ self

Parameters:

  • template (String)


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

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.



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

def all
  @all
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

#unknownObject (readonly)

Returns the value of attribute unknown.



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

def unknown
  @unknown
end

#validObject (readonly)

Returns the value of attribute valid.



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

def valid
  @valid
end

Instance Method Details

#gsub(values) ⇒ String (frozen)

Parameters:

  • values (Hash{Symbol->String)

    ]

Returns:

  • (String (frozen))


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

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

#typesArray<String>

Returns:

  • (Array<String>)


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

def types
  RENAME_TYPES.keys
end

#unknown_listString

Returns:

  • (String)


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

def unknown_list
  unknown.join(", ")
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  unknown.empty?
end