module Rmk::Utility
collection of utilities used by Rmk
Public Instance Methods
first_existing_file(files)
click to toggle source
return first of files that exists (or nil)
# File lib/utilities.rb, line 160 def first_existing_file(files) files.find { |f| File.exist?(f) } end
grep(pattern, files)
click to toggle source
return all files whose contents match pattern
# File lib/utilities.rb, line 109 def grep(pattern, files) @@grep_sources ||= {} @@grep_patterns_found ||= {} files.find_all do |src| @@grep_sources[src] = IO.read(src) unless @@grep_sources[src] match = begin @@grep_sources[src] =~ pattern rescue StandardError #=> e Rmk.logger.warn "Pattern matching failed for file '#{src}'. "\ 'Check encoding!' @@grep_sources[src].force_encoding('iso-8859-1') .encode('utf-8') =~ pattern end if match @@grep_patterns_found[pattern] = src # cache this! true else false end end end
spawn(cmd)
click to toggle source
#spawn command
# File lib/utilities.rb, line 172 def spawn(cmd) return true if system(cmd) raise "'#{cmd}' failed (#{$?})" end
stable_unique(items)
click to toggle source
make items unique without changing order
# File lib/utilities.rb, line 134 def stable_unique(items) # TODO: Array#uniq does this! (uses a Hash and does not sort/require sorting!) found = {} items.find_all do |x| if found[x] false else found[x] = x end end end
stable_unique_paths(paths)
click to toggle source
make paths unique without changing order
# File lib/utilities.rb, line 147 def stable_unique_paths(paths) found = {} paths.find_all do |p| ap = File.expand_path(p) if found[ap] false else found[ap] = p end end end
strip_extension(name)
click to toggle source
get file name without extension
# File lib/utilities.rb, line 103 def strip_extension(name) name =~ /(.*)\.[^.]+$/ $1 || name end
word_wrap(text, col_width)
click to toggle source
break text into lines of maximum col_width
# File lib/utilities.rb, line 165 def word_wrap(text, col_width) text.gsub!(/(\S{#{col_width}})(?=\S)/, '\1 ') text.gsub!(/(.{1,#{col_width}})(?:\s+|$)/, "\\1\n") text end