Class: Cleanliness::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/cleanliness/dictionary.rb

Class Method Summary collapse

Class Method Details

.initializeObject



4
5
6
7
# File 'lib/cleanliness/dictionary.rb', line 4

def self.initialize
  @dictionary ||= YAML.load_file(File.join(File.dirname(__FILE__), '', 'dictionary.yml'))
  return @dictionary
end

.translate(string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cleanliness/dictionary.rb', line 9

def self.translate(string)
  @dictionary = initialize
  replace ={}
  rgx = Regexp.new "#{@dictionary.keys.map{|k| "(#{k})"}.join("|")}", Regexp::IGNORECASE
  scan_matches = string.scan(rgx).flatten.reject!(&:blank?)
  return string if scan_matches.nil?
  scan_matches.each{|k|
    replace[k] = (k =~ /^[A-Z]{1}/).nil? ? @dictionary[k.downcase] : @dictionary[k.downcase].capitalize
  }
  match = Regexp.new scan_matches.map{|k| "(#{k})"}.join("|")
  return string.gsub(match, replace)
end