Class: Peanuts::Converter::Convert_string
- Inherits:
-
Object
- Object
- 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
Constructor Details
#initialize(options) ⇒ Convert_string
Returns a new instance of Convert_string.
45 46 47 |
# File 'lib/peanuts/converters.rb', line 45 def initialize() @whitespace = [:whitespace] || :collapse end |
Instance Method Details
#from_xml(string) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/peanuts/converters.rb', line 53 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
49 50 51 |
# File 'lib/peanuts/converters.rb', line 49 def to_xml(string) string end |