Module: DefMastership::RenameIncludedFilesModifier::Helper

Defined in:
lib/defmastership/rename_included_files_modifier.rb

Overview

Helper functions

Class Method Summary collapse

Class Method Details

.apply_filters_until_consumed(line) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/defmastership/rename_included_files_modifier.rb', line 172

def self.apply_filters_until_consumed(line)
  LOCAL_FILTERS.each do |filter|
    next unless line.match(filter.regexp)

    yield(filter.event, Regexp.last_match)
    break
  end
end

.complete_regexp_from(from) ⇒ Object



153
154
155
156
157
158
# File 'lib/defmastership/rename_included_files_modifier.rb', line 153

def self.complete_regexp_from(from)
  Regexp.new(
    DMRegexp::INCLUDE_KEYWORD + DMRegexp::INCLUDE_PATH +
    "(?<filename>#{from})" + DMRegexp::INCLUDE_OPTIONS
  )
end

.extract_filename(include_statement, variables) ⇒ Object



128
129
130
131
132
# File 'lib/defmastership/rename_included_files_modifier.rb', line 128

def self.extract_filename(include_statement, variables)
  filename = filename_from_include_statement(include_statement)

  filename_replace_all_variables(filename, variables).chomp
end

.filename_from_include_statement(include_statement) ⇒ Object



134
135
136
137
138
# File 'lib/defmastership/rename_included_files_modifier.rb', line 134

def self.filename_from_include_statement(include_statement)
  include_statement
    .sub(Regexp.new(DMRegexp::INCLUDE_KEYWORD), '')
    .sub(Regexp.new(DMRegexp::INCLUDE_OPTIONS), '')
end

.filename_replace_all_variables(filename, variables) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/defmastership/rename_included_files_modifier.rb', line 140

def self.filename_replace_all_variables(filename, variables)
  filename.scan(DMRegexp::VARIABLE_USE) do
    varname = Regexp.last_match[:varname]
    value = variables.fetch(varname.to_sym)
    filename = filename_replace_one_variable(filename, varname, value)
  end
  filename
end

.filename_replace_one_variable(filename, varname, value) ⇒ Object



149
150
151
# File 'lib/defmastership/rename_included_files_modifier.rb', line 149

def self.filename_replace_one_variable(filename, varname, value)
  filename.sub("{#{varname}}", value)
end

.hmerge(config, match) ⇒ Object



164
165
166
# File 'lib/defmastership/rename_included_files_modifier.rb', line 164

def self.hmerge(config, match)
  config.merge(match.names.map(&:to_sym).zip(match.captures).to_h)
end

.text_with(match, to) ⇒ Object



160
161
162
# File 'lib/defmastership/rename_included_files_modifier.rb', line 160

def self.text_with(match, to)
  "include::#{match[:path]}#{to}[#{match[:options]}]"
end

.variable_def_hash(match) ⇒ Object



168
169
170
# File 'lib/defmastership/rename_included_files_modifier.rb', line 168

def self.variable_def_hash(match)
  { match[:varname].to_sym => match[:value] }
end