Module: Normatron::Filters::SqueezeFilter

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

Overview

Remove multiple occurences of the same character.

If no option are given, all runs of identical characters are replaced by a single character.

Examples:

Out of box

SqueezeFilter.call("yellow    moon")             #=> "yelow mon"
SqueezeFilter.call("  now   is  the", " ")       #=> " now is the"
SqueezeFilter.call("putters shoot balls", "m-z") #=> "puters shot balls"

Using as ActiveRecord::Base normalizer

normalize :attribute_a, :with => [:custom_filter, :squeeze]
normalize :attribute_b, :with => [:custom_filter, [:squeeze, "a-f"]]
normalize :attribute_c, :with => [:custom_filter, {:squeeze => ["a-f"]}]

See Also:

Class Method Summary collapse

Class Method Details

.call(input, *targets) ⇒ 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

  • targets ([String]*)

    Characters to be affected

Returns:

  • (String)

    A new squeezed String



32
33
34
35
# File 'lib/normatron/filters/squeeze_filter.rb', line 32

def self.call(input, *targets)
  return input unless input.kind_of?(String)
  targets.any? ? input.squeeze(targets.last) : input.squeeze
end