Module: Normatron::Filters::SwapcaseFilter

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

Overview

Replaces uppercased characters by lowercased and vice versa.

Examples:

Out of box

SwapcaseFilter.call("As you Wish!") #=> "aS YOU wISH!"

Using as ActiveRecord::Base normalizer

normalize :attribute_a, :with => :swapcase
normalize :attribute_b, :with => [:custom_filter, :swapcase]

See Also:

Class Method Summary collapse

Methods included from Helpers

acronym_regex, acronyms, evaluate_regexp, inflections, mb_send

Class Method Details

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

Returns:

  • (String)

    A new swapcased String



30
31
32
33
# File 'lib/normatron/filters/swapcase_filter.rb', line 30

def self.call(input)
  return input unless input.kind_of?(String)
  input.gsub(/([\p{Ll}])|(\p{Lu})|([^\p{Ll}\p{Lu}])/u) { $3 || ($2 ? mb_send(:downcase, $2) : mb_send(:upcase, $1)) }
end