Module: Fixnames::Helpers

Included in:
Engine
Defined in:
lib/fixnames/helpers.rb

Instance Method Summary collapse

Instance Method Details

#match_bracket_closeObject



40
41
42
# File 'lib/fixnames/helpers.rb', line 40

def match_bracket_close
  "[#{Regexp.escape(option.bracket_characters_close)}]"
end

#match_bracket_openObject



36
37
38
# File 'lib/fixnames/helpers.rb', line 36

def match_bracket_open
  "[#{Regexp.escape(option.bracket_characters_open)}]"
end

#remove(re) ⇒ Object



17
18
19
20
21
# File 'lib/fixnames/helpers.rb', line 17

def remove(re)
  re_str = bold "/#{re}/"
  debug "\t<expunge>  #{re_str}"
  fixed.gsub! Regexp.new(re), ''
end

#remove_all(condition) ⇒ Object



27
28
29
# File 'lib/fixnames/helpers.rb', line 27

def remove_all(condition)
  safeloop(condition) { remove(condition) }
end

#remove_bracket_characters_from(str) ⇒ Object



52
53
54
# File 'lib/fixnames/helpers.rb', line 52

def remove_bracket_characters_from(str)
  str.gsub /(#{match_bracket_open}|#{match_bracket_close})/, ''
end

#remove_bracket_ranges(re) ⇒ Object



48
49
50
# File 'lib/fixnames/helpers.rb', line 48

def remove_bracket_ranges(re)
  remove wrap_brackets(".*?#{re}.*?")
end

#replace(re, replacement) ⇒ Object



10
11
12
13
14
15
# File 'lib/fixnames/helpers.rb', line 10

def replace(re, replacement)
  re_str = bold "/#{re}/"
  replacement_str = bold "\"#{replacement}\""
  debug "\t<replace>  #{re_str}  ->  #{replacement_str}"
  fixed.gsub! Regexp.new(re), replacement
end

#replace_all(condition, replacement) ⇒ Object



23
24
25
# File 'lib/fixnames/helpers.rb', line 23

def replace_all(condition, replacement)
  safeloop(condition) { replace(condition, replacement) }
end

#safeloop(condition, max_tries = option.max_filter_loops, &block) ⇒ Object



3
4
5
6
7
8
# File 'lib/fixnames/helpers.rb', line 3

def safeloop(condition, max_tries = option.max_filter_loops, &block)
  max_tries.times do
    return fixed unless fixed =~ Regexp.new(condition)
    block.call
  end
end

#translate(src, dst) ⇒ Object



31
32
33
34
# File 'lib/fixnames/helpers.rb', line 31

def translate(src, dst)
  debug "\t<translate>  #{bold src.inspect}  ->  #{bold dst.inspect}"
  fixed.tr! src, dst
end

#wrap_brackets(re) ⇒ Object



44
45
46
# File 'lib/fixnames/helpers.rb', line 44

def wrap_brackets(re)
  "#{match_bracket_open}#{re}#{match_bracket_close}"
end