Module: Normatron::Filters::StripFilter

Extended by:
Helpers
Defined in:
lib/normatron/filters/strip_filter.rb

Overview

Removes traling and leading spaces.

Examples:

Out of box

StripFilter.evaluate("   copy   ")      #=> "copy"
StripFilter.evaluate("   copy   ", :L)  #=> "copy   "
StripFilter.evaluate("   copy   ", :R)  #=> "   copy"
StripFilter.evaluate("   copy   ", :LR) #=> "copy"

Using as ActiveRecord::Base normalizer

normalize :attribute_a, :with => :strip
normalize :attribute_b, :with => { :strip => :L }
normalize :attribute_c, :with => [:custom_filter, :strip]
normalize :attribute_d, :with => [:custom_filter, [:strip, :L]]
normalize :attribute_e, :with => [:custom_filter, {:strip => :R}]

See Also:

Class Method Summary collapse

Methods included from Helpers

acronym_regex, acronyms, evaluate_regexp, inflections, mb_send

Class Method Details

.evaluate(input, edges = :LR) ⇒ 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

  • edges (Symbol) (defaults to: :LR)

    :L to strip trailing spaces, :R for leading spaces or :LR for both

Returns:

  • (String)

    A new stripped String



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/normatron/filters/strip_filter.rb', line 36

def self.evaluate(input, edges=:LR)
  return input unless input.kind_of?(String)
  
  regex_string = 
    case edges
      when :L
        '\A\s*'
      when :R
        '\s*\z'
      when :LR
        '\A\s*|\s*\z'
    end
  regex = Regexp.new(/#{regex_string}/)
  input.gsub(regex, '')
end