Class: Defmastership::Modifier::ChangeRef

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

Overview

Change references from temporary to definitive with multiple RefChangers

Defined Under Namespace

Modules: Helper

Instance Attribute Summary

Attributes included from ModifierCommon

#changes, #config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModifierCommon

#do_modifications, #method_missing, #respond_to_missing?, #setup_modifier_module

Constructor Details

#initialize(config) ⇒ ChangeRef

Returns a new instance of ChangeRef.

Parameters:

  • config (YAML)

    the modifier’s provided configurations



44
45
46
47
48
# File 'lib/defmastership/modifier/change_ref.rb', line 44

def initialize(config)
  @parsing_state = Core::ParsingState.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

Class Method Details

.default_configHash{Symbol => Object}

Returns the default configuration.

Returns:

  • (Hash{Symbol => Object})

    the default configuration



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

def self.default_config
  {
    from_regexp: '',
    to_template: '',
    next_ref: 0
  }
end

.replacement_methodsArray<Symbol>

Methods’s symbols will be called in sequence to perform the document modifications

Returns:

  • (Array<Symbol>)

    the two symbols of replacement methods



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

def self.replacement_methods
  %i[replace_refdef replace_irefs]
end

Instance Method Details

#replace_irefs(line) ⇒ String

Replace the definition’s refs in intenal refs

Parameters:

  • line (String)

    the current line

Returns:

  • (String)

    the modified line



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

def replace_irefs(line)
  changes.reduce(line) do |res_line, (from, to)|
    res_line.gsub(Helper.regexp_from(:iref, from)) do
      Helper.text_with(Regexp.last_match, to)
    end
  end
end

#replace_refdef(line) ⇒ String

Replace the definition’s ref in the line if relevant

Parameters:

  • line (String)

    the current line

Returns:

  • (String)

    the modified line



54
55
56
57
58
59
60
# File 'lib/defmastership/modifier/change_ref.rb', line 54

def replace_refdef(line)
  if @parsing_state.enabled?(line)
    do_replace_refdef(line)
  else
    line
  end
end