Class: Peanuts::Converter::Convert_string

Inherits:
Peanuts::Converter show all
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.

Instance Method Summary collapse

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(options)
  @whitespace = options[: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