class VacuumCleaner::Normalizations::MethodNormalizer

Generic method based normalizer which just calls supplied method on value (unless nil).

normalizes :name, :method => :titelize

Custom instances accept a :method option.

MethodNormalizer.new(:method => :titelize)

Subclasses of the MethodNormalizer can take advantage of it's normalize_if_respond_to method, to easily create custom normalizers based on methods availble on the result value.

Public Class Methods

build(sym) click to toggle source

Helper method to “bake” a method normalizer from a method, enabling us to do stuff like.

TitelizeNormalizer = MethodNormalizer.build(:titleize)
# File lib/vacuum_cleaner/normalizations/method.rb, line 23
def build(sym)
  module_eval "Class.new(MethodNormalizer) do; def initialize(*args); super({ :method => #{sym.inspect}}) end; end", __FILE__, __LINE__
end
multibyte_wrapper() click to toggle source
# File lib/vacuum_cleaner/normalizations/method.rb, line 35
def multibyte_wrapper; @multibyte_wrapper end
multibyte_wrapper=(clazz) click to toggle source

Due to the lack of multibyte support in ruby core, a proxy class like {ActiveSupport::Multibyte::Chars} can be registered here and the proxy is then used to wrap string values within, so that methods like `upcase` or `downcase` work with characters outside the ASCII range as well.

The wrapper is only used if the value supplied is a string, i.e. responds to to_str.

# File lib/vacuum_cleaner/normalizations/method.rb, line 34
def multibyte_wrapper=(clazz); @multibyte_wrapper = clazz end
new(args = {}) click to toggle source

Accept either a hash or symbol name.

Calls superclass method
# File lib/vacuum_cleaner/normalizations/method.rb, line 39
def initialize(args = {})
  args = { :method => args } unless args.is_a?(Hash)
  super(args)
end

Public Instance Methods

normalize_value(value) click to toggle source

Normalize value by trying to call the method at hand, if value does not respond to the defined method, returns nil.

# File lib/vacuum_cleaner/normalizations/method.rb, line 46
def normalize_value(value)
  sym = options[:method]
  value = MethodNormalizer.multibyte_wrapper.new(value) if MethodNormalizer.multibyte_wrapper and value.respond_to?(:to_str)
  value.respond_to?(sym) ? value.send(sym) : nil
end