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



37
38
39
40
41
# File 'lib/defmastership/modifier/update_def.rb', line 37

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.



13
14
15
# File 'lib/defmastership/modifier/update_def.rb', line 13

def document
  @document
end

Class Method Details

.default_configHash{Symbol => Object}

Returns the default configuration.

Returns:

  • (Hash{Symbol => Object})

    the default configuration



16
17
18
19
20
# File 'lib/defmastership/modifier/update_def.rb', line 16

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



32
33
34
# File 'lib/defmastership/modifier/update_def.rb', line 32

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



23
24
25
# File 'lib/defmastership/modifier/update_def.rb', line 23

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



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

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

  super
end

#replace_reference(line) ⇒ String

Called on each line for an opportunity for text replacement

Parameters:

  • line (String)

    line from asciidoc sources files

Returns:

  • (String)

    the modified line



61
62
63
64
65
66
67
68
69
# File 'lib/defmastership/modifier/update_def.rb', line 61

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

  return line unless match
  return line unless match[:type] == def_type

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