Class: Peanuts::Converter::Convert_string

Inherits:
Object
  • Object
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

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