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

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



73
74
75
76
77
# File 'lib/defmastership/modifier/change_ref.rb', line 73

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



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

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



59
60
61
# File 'lib/defmastership/modifier/change_ref.rb', line 59

def self.replacement_methods
  i[replace_refdef replace_irefs replace_include_tags replace_tags_in_included_files]
end

Instance Method Details

#replace_include_tags(line) ⇒ String

Replace the definition’s refs in tags of include statements

Parameters:

  • line (String)

    the current line

Returns:

  • (String)

    the modified line



107
108
109
110
111
112
113
# File 'lib/defmastership/modifier/change_ref.rb', line 107

def replace_include_tags(line)
  return line unless line.match?(Core::DMRegexp::INCLUDE)

  changes.reduce(line) do |res_line, (from, to)|
    res_line.sub(/(?<before>tags?=!?([^;]*;)?)#{from}\b/) { "#{$LAST_MATCH_INFO[:before]}#{to}" }
  end
end

#replace_irefs(line) ⇒ String

Replace the definition’s refs in intenal refs

Parameters:

  • line (String)

    the current line

Returns:

  • (String)

    the modified line



95
96
97
98
99
100
101
# File 'lib/defmastership/modifier/change_ref.rb', line 95

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



83
84
85
86
87
88
89
# File 'lib/defmastership/modifier/change_ref.rb', line 83

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

#replace_tags_in_included_files(line) ⇒ String

Replace the definition’s refs in included files

Parameters:

  • line (String)

    the current line

Returns:

  • (String)

    the (unmodified) line



119
120
121
122
123
124
# File 'lib/defmastership/modifier/change_ref.rb', line 119

def replace_tags_in_included_files(line)
  if line.match(Core::DMRegexp::INCLUDE)
    Helper.replace_tags_in_included_files(Helper::ParsedFilename.full_filename(Regexp.last_match), changes)
  end
  line
end