Class: Grawlix::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/grawlix/translator.rb

Constant Summary collapse

DEFAULTS =
{
  memoize: true,
  glyphs: "!$%*&@#£¥€Þ§¢~^?",
  ignore: /^\s+$/,
  noise: 0.1
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Translator

Returns a new instance of Translator.



10
11
12
13
14
15
16
# File 'lib/grawlix/translator.rb', line 10

def initialize(opts = {})
  @cache = GlyphSizeCache.new      
  @opts = DEFAULTS.merge(opts)
  @glyph_dict = @opts[:glyphs].split('').each_with_object({}) { |g, h| (h[@cache.width_for(g)] ||= []) << g }.to_a.sort_by { |p| p[0] }
  @widths, @glyphs = @glyph_dict.transpose
  @memo = {}
end

Instance Method Details

#translate(input) ⇒ Object



18
19
20
# File 'lib/grawlix/translator.rb', line 18

def translate(input)
  input.split('').map { |c| c =~ @opts[:ignore] ? c : glyph_for(c) }.join
end