Class: VacuumCleaner::Normalizer
- Inherits:
-
Object
- Object
- VacuumCleaner::Normalizer
- Defined in:
- lib/vacuum_cleaner/normalizer.rb
Overview
A small base class for implementing custom value normalizers. Might seem like a slight overkill, yet makes the library pretty reusable and all. Works like ActiveModel validators.
class TitleizeNormalizer < VacuumCleaner::Normalizer
def normalize_value(value)
value.titelize unless value.blank?
end
end
class Person
include VacuumCleaner::Normalizations
normalizes :name, :titleize => true
end
Any class that inherits from VacuumCleaner::Normalizer must implement a method called normalize_value which accepts the value to normalize. Furthermore the value returned by normalize_value is used as the new value for the attribute.
To reuse the behaviour as defined by the default normalizer (strip & empty), just use super.
class MoreNilNormalizer < VacuumCleaner::Normalizer
def normalize_value(value)
value = super
value = value.downcase if value.respond_to(:downcase)
%w{0 nil null nul zero nix}.include?(value) ? nil : value
end
end
If access to the record or attribute being normalized is required the method normalize can be overriden instead.
class FancyNormalizer < VacuumCleaner::Normalizer
def normalize(object, attribute, value)
...
end
end
When the normalization process is started for an attribute, the method normalize is called with the object, attribute and the value. The default implementation of normalize (as provided by this class) just calls normalize_value.
This can be used together with the normalizes method (see VacuumCleaner::Normalizers#normalizes for more on this).
Direct Known Subclasses
VacuumCleaner::Normalizations::MethodNormalizer, VacuumCleaner::Normalizations::NumericNormalizer, VacuumCleaner::Normalizations::TransliterateNormalizer, VacuumCleaner::Normalizations::UrlNormalizer
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Options as supplied to the normalizer.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Normalizer
constructor
Accepts an array of options, which will be made available through the
optionsreader. -
#normalize(object, attribute, value) ⇒ Object
Only override this method if access to the
objectorattributename is required, else overridenormalize_value, makes life much simpler :). -
#normalize_value(value) ⇒ Object
Override this method in subclasses to specifiy custom normalization steps and magic.
Constructor Details
#initialize(options = {}) ⇒ Normalizer
Accepts an array of options, which will be made available through the options reader.
55 56 57 |
# File 'lib/vacuum_cleaner/normalizer.rb', line 55 def initialize( = {}) = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Options as supplied to the normalizer.
52 53 54 |
# File 'lib/vacuum_cleaner/normalizer.rb', line 52 def end |
Instance Method Details
#normalize(object, attribute, value) ⇒ Object
Only override this method if access to the object or attribute name is required, else override normalize_value, makes life much simpler :)
Default behaviour just calls #normalize_value.
63 |
# File 'lib/vacuum_cleaner/normalizer.rb', line 63 def normalize(object, attribute, value); normalize_value(value) end |
#normalize_value(value) ⇒ Object
Override this method in subclasses to specifiy custom normalization steps and magic.
The standard implementation strips the value of trailing/leading whitespace and then either returns that value or nil if it’s empty?.
69 70 71 72 |
# File 'lib/vacuum_cleaner/normalizer.rb', line 69 def normalize_value(value) value = value.strip if value.respond_to?(:strip) value.nil? || (value.respond_to?(:empty?) && value.empty?) ? nil : value end |