Class: Defmastership::Modifier::UpdateDef Abstract

Inherits:
Object
  • Object
show all
Includes:
ModifierCommon
Defined in:
lib/defmastership/modifier/update_def.rb

Overview

This class is abstract.

Subclass and define reference_replacement to implement a

custom reference replacement class

Direct Known Subclasses

UpdateDefChecksum, UpdateDefVersion

Instance Attribute Summary collapse

Attributes included from ModifierCommon

#changes, #config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModifierCommon

#method_missing, #respond_to_missing?, #setup_modifier_module

Constructor Details

#initialize(config) ⇒ UpdateDef

Returns a new instance of UpdateDef.

Parameters:

  • config (YAML)

    the modifier’s provided configurations



39
40
41
42
43
# File 'lib/defmastership/modifier/update_def.rb', line 39

def initialize(config)
  @document = Document.new

  setup_modifier_module(config)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Defmastership::Modifier::ModifierCommon

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



15
16
17
# File 'lib/defmastership/modifier/update_def.rb', line 15

def document
  @document
end

Class Method Details

.default_configHash{Symbol => Object}

Returns the default configuration.

Returns:

  • (Hash{Symbol => Object})

    the default configuration



18
19
20
21
22
# File 'lib/defmastership/modifier/update_def.rb', line 18

def self.default_config
  {
    def_type: ''
  }
end

.reference_regexp(reference) ⇒ Regexp

Builds a Regexp to match a particular reference defintion (with its optional version and checksum)

Parameters:

  • reference (String)

    the reference

Returns:

  • (Regexp)

    the built Regexp



34
35
36
# File 'lib/defmastership/modifier/update_def.rb', line 34

def self.reference_regexp(reference)
  Regexp.new("#{reference}#{Core::DMRegexp::DEF_VERSION_AND_CHECKSUM}")
end

.replacement_methodsArray<Symbol>

Returns the methods’s symbols that will be called in sequence for modifications.

Returns:

  • (Array<Symbol>)

    the methods’s symbols that will be called in sequence for modifications



25
26
27
# File 'lib/defmastership/modifier/update_def.rb', line 25

def self.replacement_methods
  %i[replace_reference]
end

Instance Method Details

#do_modifications(adoc_sources) ⇒ Object

Apply the modifier on all provided asciidoc sources based on modifier’s self.replacement_methods list

Parameters:

  • adoc_sources (Hash{String => String})

    asciidoc sources

    • :key filename

    • :value file content



51
52
53
54
55
56
57
# File 'lib/defmastership/modifier/update_def.rb', line 51

def do_modifications(adoc_sources)
  adoc_sources.each_key do |adoc_file|
    document.parse_file_with_preprocessor(adoc_file)
  end

  super
end

#replace_reference(_filename, line) ⇒ String

Called on each line for an opportunity for text replacement

Parameters:

  • _filename (String)

    the filename of the file beeing modified

  • line (String)

    line from asciidoc sources files

Returns:

  • (String)

    the modified line



64
65
66
67
68
69
70
71
72
# File 'lib/defmastership/modifier/update_def.rb', line 64

def replace_reference(_filename, line)
  match = line.match(Core::DMRegexp::DEFINITION)

  return line unless match
  return line unless DefTypeList.new(def_type).include?(match[:type])

  reference = match[:reference]
  line.sub(self.class.reference_regexp(reference), reference_replacement(reference, match))
end