Class: Stringex::Localization::Converter

Inherits:
Object
  • Object
show all
Includes:
ConversionExpressions
Defined in:
lib/stringex/localization/converter.rb

Constant Summary

Constants included from ConversionExpressions

Stringex::Localization::ConversionExpressions::ABBREVIATION, Stringex::Localization::ConversionExpressions::ACCENTED_HTML_ENTITY, Stringex::Localization::ConversionExpressions::APOSTROPHE, Stringex::Localization::ConversionExpressions::CHARACTERS, Stringex::Localization::ConversionExpressions::CLEANUP_CHARACTERS, Stringex::Localization::ConversionExpressions::CLEANUP_HTML_ENTITIES, Stringex::Localization::ConversionExpressions::CURRENCIES, Stringex::Localization::ConversionExpressions::CURRENCIES_COMPLEX, Stringex::Localization::ConversionExpressions::CURRENCIES_SIMPLE, Stringex::Localization::ConversionExpressions::CURRENCIES_SUPPORTED, Stringex::Localization::ConversionExpressions::CURRENCIES_SUPPORTED_COMPLEX, Stringex::Localization::ConversionExpressions::CURRENCIES_SUPPORTED_SIMPLE, Stringex::Localization::ConversionExpressions::HTML_ENTITIES, Stringex::Localization::ConversionExpressions::HTML_TAG, Stringex::Localization::ConversionExpressions::SMART_PUNCTUATION, Stringex::Localization::ConversionExpressions::UNREADABLE_CONTROL_CHARACTERS, Stringex::Localization::ConversionExpressions::VULGAR_FRACTIONS, Stringex::Localization::ConversionExpressions::WHITESPACE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options = {}) ⇒ Converter

Returns a new instance of Converter.



12
13
14
15
16
17
18
19
# File 'lib/stringex/localization/converter.rb', line 12

def initialize(string, options = {})
  @string = string.dup
  @options = Stringex::Configuration::StringExtensions.default_settings.merge(options)
  string =~ /^(\s+)/
  @starting_whitespace = $1 unless $1 == ''
  string =~ /(\s+)$/
  @ending_whitespace = $1 unless $1 == ''
end

Instance Attribute Details

#ending_whitespaceObject (readonly)

Returns the value of attribute ending_whitespace.



10
11
12
# File 'lib/stringex/localization/converter.rb', line 10

def ending_whitespace
  @ending_whitespace
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/stringex/localization/converter.rb', line 10

def options
  @options
end

#starting_whitespaceObject (readonly)

Returns the value of attribute starting_whitespace.



10
11
12
# File 'lib/stringex/localization/converter.rb', line 10

def starting_whitespace
  @starting_whitespace
end

#stringObject (readonly)

Returns the value of attribute string.



10
11
12
# File 'lib/stringex/localization/converter.rb', line 10

def string
  @string
end

Instance Method Details

#cleanup_accented_html_entities!Object



21
22
23
# File 'lib/stringex/localization/converter.rb', line 21

def cleanup_accented_html_entities!
  string.gsub! expressions.accented_html_entity, '\1'
end

#cleanup_characters!Object



25
26
27
# File 'lib/stringex/localization/converter.rb', line 25

def cleanup_characters!
  string.gsub! expressions.cleanup_characters, ' '
end

#cleanup_html_entities!Object



29
30
31
# File 'lib/stringex/localization/converter.rb', line 29

def cleanup_html_entities!
  string.gsub! expressions.cleanup_html_entities, ''
end

#cleanup_smart_punctuation!Object



33
34
35
36
37
# File 'lib/stringex/localization/converter.rb', line 33

def cleanup_smart_punctuation!
  expressions.smart_punctuation.each do |expression, replacement|
    string.gsub! expression, replacement
  end
end

#normalize_currency!Object



39
40
41
# File 'lib/stringex/localization/converter.rb', line 39

def normalize_currency!
  string.gsub!(/(\d+),(\d+)/, '\1\2')
end

#smart_strip!Object



43
44
45
46
# File 'lib/stringex/localization/converter.rb', line 43

def smart_strip!
  string.strip!
  @string = "#{starting_whitespace}#{string}#{ending_whitespace}"
end

#strip!Object



48
49
50
# File 'lib/stringex/localization/converter.rb', line 48

def strip!
  string.strip!
end

#strip_html_tags!Object



52
53
54
# File 'lib/stringex/localization/converter.rb', line 52

def strip_html_tags!
  string.gsub! expressions.html_tag, ''
end

#translate!(*conversions) ⇒ Object



56
57
58
59
60
# File 'lib/stringex/localization/converter.rb', line 56

def translate!(*conversions)
  conversions.each do |conversion|
    send conversion
  end
end