Module: Normatron::Filters::SwapcaseFilter

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

Class Method Summary collapse

Methods included from Helpers

acronym_regex, acronyms, evaluate_regexp, evaluate_strip, inflections, mb_send

Class Method Details

.evaluate(input) ⇒ String

Replaces uppercased characters by lowercased and vice versa.

Examples:

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

Using as ActiveRecord::Base normalizer

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

Parameters:

  • input (String)

    A character sequence

Returns:

  • (String)

    The swapped case character sequence or the object itself

See Also:



24
25
26
27
# File 'lib/normatron/filters/swapcase_filter.rb', line 24

def self.evaluate(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