Class: Kiba::Extend::Utils::StringNormalizer
- Inherits:
- 
      Object
      
        - Object
- Kiba::Extend::Utils::StringNormalizer
 
- Defined in:
- lib/kiba/extend/utils/string_normalizer.rb
Overview
Normalizes the given string according to the given parameters.
Can be used two ways. Preferred method when using in a transform or other context when the same normalization settings will be used to normalize many strings:
  # first initialize an instance of the class as an instance variable in
  #   your context
  @normalizer = StringNormalizer.new(downcased: false)
  # for the repetitive part:
  vals.each{ |val| @normalizer.call(val) }
For one-off usage, or where the normalization settings vary per normalized value, you can do:
StringNormalizer.call(downcased: false, str: 'Table, café')
  => 'Tablecafe'
The second way is much less performant, as it initializes a new instance of the class every time it is called.
Class Method Summary collapse
Instance Method Summary collapse
- 
  
    
      #call(val)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #initialize(mode: :plain, downcased: true)  ⇒ StringNormalizer 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of StringNormalizer. 
Constructor Details
#initialize(mode: :plain, downcased: true) ⇒ StringNormalizer
Returns a new instance of StringNormalizer.
| 110 111 112 113 114 | # File 'lib/kiba/extend/utils/string_normalizer.rb', line 110 def initialize(mode: :plain, downcased: true) @mode = mode @downcased = downcased @subs = set_subs end | 
Class Method Details
.call(str:, mode: :plain, downcased: true) ⇒ Object
| 101 102 103 | # File 'lib/kiba/extend/utils/string_normalizer.rb', line 101 def call(str:, mode: :plain, downcased: true) new(mode: mode, downcased: downcased).call(str) end | 
Instance Method Details
#call(val) ⇒ Object
| 116 117 118 119 120 121 122 123 124 125 | # File 'lib/kiba/extend/utils/string_normalizer.rb', line 116 def call(val) unless val.unicode_normalized?(:nfkc) val = val.unicode_normalize(:nfkc) end subs.each { |old, new| val = val.gsub(old, new) } val = ActiveSupport::Inflector.transliterate(val).gsub(/\W/, "") downcased ? val.downcase : val end |