Module: Normatron::Filters::ChompFilter

Defined in:
lib/normatron/filters/chomp_filter.rb

Overview

Remove the given record separator from the end of the string (If present). If $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).

Examples:

Out of box

ChompFilter.call("Bon Scott\n")         #=> "Bon Scott"
ChompFilter.call("Bon Scott\r")         #=> "Bon Scott"
ChompFilter.call("Bon Scott\r\n")       #=> "Bon Scott"
ChompFilter.call("Bon Scott\n\r")       #=> "Bon Scott\n"
ChompFilter.call("Bon Scott", " Scott") #=> "Bon"

Using as model normalizer

normalize :attribute_a, :with => :chomp
normalize :attribute_b, :with => [:custom_filter, :chomp]
normalize :attribute_c, :with => [[:chomp, "x"]]
normalize :attribute_d, :with => [{:chomp => "y"}]
normalize :attribute_e, :with => [:custom_filter, [:chomp, "z"]]
normalize :attribute_f, :with => [:custom_filter, {:chomp => "\f"}]

See Also:

Class Method Summary collapse

Class Method Details

.call(input, separator = $/) ⇒ String

Performs input conversion according to filter requirements.

This method returns the object itself when the first argument is not a String.

Parameters:

  • input (String)

    The String to be filtered

  • separator (String) (defaults to: $/)

    The separator used to chomp input

Returns:

  • (String)

    A new chopped String



35
36
37
# File 'lib/normatron/filters/chomp_filter.rb', line 35

def self.call(input, separator=$/)
  input.kind_of?(String) ? input.chomp(separator) : input
end