Class: VacuumCleaner::Normalizations::MethodNormalizer
- Inherits:
-
VacuumCleaner::Normalizer
- Object
- VacuumCleaner::Normalizer
- VacuumCleaner::Normalizations::MethodNormalizer
- Defined in:
- lib/vacuum_cleaner/normalizations/method.rb
Overview
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.
Instance Attribute Summary
Attributes inherited from VacuumCleaner::Normalizer
Class Method Summary collapse
-
.build(sym) ⇒ Object
Helper method to “bake” a method normalizer from a method, enabling us to do stuff like.
- .multibyte_wrapper ⇒ Object
-
.multibyte_wrapper=(clazz) ⇒ Object
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
upcaseordowncasework with characters outside the ASCII range as well.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ MethodNormalizer
constructor
Accept either a hash or symbol name.
-
#normalize_value(value) ⇒ Object
Normalize value by trying to call the method at hand, if
valuedoes not respond to the defined method, returnsnil.
Methods inherited from VacuumCleaner::Normalizer
Constructor Details
#initialize(args = {}) ⇒ MethodNormalizer
Accept either a hash or symbol name.
39 40 41 42 |
# File 'lib/vacuum_cleaner/normalizations/method.rb', line 39 def initialize(args = {}) args = { :method => args } unless args.is_a?(Hash) super(args) end |
Class Method Details
.build(sym) ⇒ Object
Helper method to “bake” a method normalizer from a method, enabling us to do stuff like.
TitelizeNormalizer = MethodNormalizer.build(:titleize)
23 24 25 |
# 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 ⇒ Object
35 |
# File 'lib/vacuum_cleaner/normalizations/method.rb', line 35 def multibyte_wrapper; @multibyte_wrapper end |
.multibyte_wrapper=(clazz) ⇒ Object
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.
34 |
# File 'lib/vacuum_cleaner/normalizations/method.rb', line 34 def multibyte_wrapper=(clazz); @multibyte_wrapper = clazz end |
Instance Method Details
#normalize_value(value) ⇒ Object
Normalize value by trying to call the method at hand, if value does not respond to the defined method, returns nil.
46 47 48 49 50 |
# File 'lib/vacuum_cleaner/normalizations/method.rb', line 46 def normalize_value(value) sym = [: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 |