Class: L10nizer::KeyGenerator
- Inherits:
-
Object
- Object
- L10nizer::KeyGenerator
- Defined in:
- lib/l10nizer/keygen.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
Returns the value of attribute namespace.
Instance Method Summary collapse
- #call(string) ⇒ Object
-
#initialize ⇒ KeyGenerator
constructor
A new instance of KeyGenerator.
- #make_safe(string) ⇒ Object
- #try(key, string) ⇒ Object
Constructor Details
#initialize ⇒ KeyGenerator
5 6 7 |
# File 'lib/l10nizer/keygen.rb', line 5 def initialize @seen = {} end |
Instance Attribute Details
#namespace ⇒ Object
Returns the value of attribute namespace.
3 4 5 |
# File 'lib/l10nizer/keygen.rb', line 3 def namespace @namespace end |
Instance Method Details
#call(string) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/l10nizer/keygen.rb', line 9 def call(string) provisional = [make_safe(namespace), make_safe(string)].compact * "." until try(provisional, string) provisional.sub!(/(?:_\d+)?$/){ |m| "_" + m.to_i.succ.to_s } end return provisional end |
#make_safe(string) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/l10nizer/keygen.rb', line 28 def make_safe(string) return nil if string.nil? safe = string. downcase. gsub(/&[a-z0-9]{1,20};/, ""). # entities gsub(/<[^>]*>/, ""). # html gsub(/[^a-z0-9]+/, "_"). # non alphanumeric slice(0, 40). gsub(/^_|_$/, "") # leading/trailing _ safe = "unknown" if safe.empty? safe end |
#try(key, string) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/l10nizer/keygen.rb', line 19 def try(key, string) if [nil, string].include?(@seen[key]) @seen[key] = string true else false end end |