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



35
36
37
38
39
# File 'lib/defmastership/modifier/update_def.rb', line 35

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.



11
12
13
# File 'lib/defmastership/modifier/update_def.rb', line 11

def document
  @document
end

Class Method Details

.default_configHash{Symbol => Object}

Returns the default configuration.

Returns:

  • (Hash{Symbol => Object})

    the default configuration



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

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



30
31
32
# File 'lib/defmastership/modifier/update_def.rb', line 30

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



21
22
23
# File 'lib/defmastership/modifier/update_def.rb', line 21

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



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

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



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

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

  return line unless match
  return line unless match[:type].eql?(def_type)

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