Module: Conversion::HumanInputString
- Defined in:
- lib/conversion/mode/human_input/core.rb
Overview
This module provides conversion to clean, stripped, strings.
<object>.convert_to(Conversion::HumanInputString)
See Conversion::HumanInputString.to_converter_proc
Class Method Summary collapse
-
.to_converter_proc ⇒ Object
Returns a Proc that converts to a clean, stripped, string.
Class Method Details
.to_converter_proc ⇒ Object
Returns a Proc that converts to a clean, stripped, string.
Conversion::HumanInputString.to_converter_proc.call("\n1 2\n3 ")
# => "1 2\n3"
Blank objects are converted to nil:
Conversion::HumanInputString.to_converter_proc.call("\n ")
# => nil
If the converted object has a :to_human_input_string method, it is used before cleaning, stripping and blank-checking. (See Date#to_human_input_string)
class Date
def to_human_input_string() ... end
end
Conversion::HumanInputString.to_converter_proc.call(Date.now)
# => "10/22/2006"
See Conversion.converter, Object#convert_to
NB: if your object has a :to_human_input_string, make yourself sure that:
object.convert_to(Conversion::HumanInputString).convert_to(object.class, :mode=>:human_input) == object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/conversion/mode/human_input/core.rb', line 55 def to_converter_proc proc do |value| begin value = value.to_human_input_string rescue NoMethodError value = value.to_s end # compact spaces and non-breaking-spaces runs value = value.gsub(/[ ]+/,' ').strip unless value.nil? if value.nil? || value.empty? nil else value end end end |