Class: Peanuts::Converter::Convert_string
- Inherits:
-
Peanuts::Converter
- Object
- Peanuts::Converter
- Peanuts::Converter::Convert_string
- Defined in:
- lib/peanuts/converters.rb
Overview
Who could have thought… a string.
- Specifier
-
:string
Options:
:whitespace => :collapse
-
Whitespace handling behavior.
:trim
-
Trim whitespace from both ends.
:collapse
-
Collapse consecutive whitespace + trim as well.
:preserve
-
Keep’em all.
Direct Known Subclasses
Convert_boolean, Convert_datetime, Convert_decimal, Convert_float, Convert_integer
Instance Method Summary collapse
- #from_xml(string) ⇒ Object
-
#initialize(options) ⇒ Convert_string
constructor
A new instance of Convert_string.
- #to_xml(string) ⇒ Object
Methods inherited from Peanuts::Converter
create, create!, lookup, lookup!
Constructor Details
#initialize(options) ⇒ Convert_string
Returns a new instance of Convert_string.
54 55 56 |
# File 'lib/peanuts/converters.rb', line 54 def initialize() @whitespace = [:whitespace] || :collapse end |
Instance Method Details
#from_xml(string) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/peanuts/converters.rb', line 62 def from_xml(string) return nil unless string string = case @whitespace when :trim then string.gsub(/\A\s*|\s*\Z/, '') when :preserve then string when :collapse then string.gsub(/\s+/, ' ').gsub(/\A\s*|\s*\Z|\s*(?=\s)/, '') end end |
#to_xml(string) ⇒ Object
58 59 60 |
# File 'lib/peanuts/converters.rb', line 58 def to_xml(string) string end |