Module: AttributeNormalizer::ClassMethods

Defined in:
lib/attribute_normalizer/model_inclusions.rb

Instance Method Summary collapse

Instance Method Details

#normalize_attributes(*attributes, &block) ⇒ Object Also known as: normalize_attribute



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/attribute_normalizer/model_inclusions.rb', line 9

def normalize_attributes(*attributes, &block)
  options = attributes.last.is_a?(::Hash) ? attributes.pop : {}

  normalizers      = [ options[:with] ].flatten.compact
  normalizers      = [ options[:before] ].flatten.compact if block_given? && normalizers.empty?
  post_normalizers = [ options[:after] ].flatten.compact if block_given?

  if normalizers.empty? && !block_given?
    normalizers = AttributeNormalizer.configuration.default_normalizers # the default normalizers
  end

  attributes.each do |attribute|
    define_method "normalize_#{attribute}" do |value|
      normalized = value

      normalizers.each do |normalizer_name|
        unless normalizer_name.kind_of?(Symbol)
          normalizer_name, options = normalizer_name.keys[0], normalizer_name[ normalizer_name.keys[0] ]
        end
        normalizer = AttributeNormalizer.configuration.normalizers[normalizer_name]
        raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer
        normalized = normalizer.respond_to?(:normalize) ? normalizer.normalize( normalized , options) : normalizer.call(normalized, options)
      end

      normalized = block_given? ? yield(normalized) : normalized

      if block_given?
        post_normalizers.each do |normalizer_name|
          unless normalizer_name.kind_of?(Symbol)
            normalizer_name, options = normalizer_name.keys[0], normalizer_name[ normalizer_name.keys[0] ]
          end
          normalizer = AttributeNormalizer.configuration.normalizers[normalizer_name]
          raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer
          normalized = normalizer.respond_to?(:normalize) ? normalizer.normalize( normalized , options) : normalizer.call(normalized, options)
        end
      end

      normalized
    end

    self.send :private, "normalize_#{attribute}"

    if method_defined?(:"#{attribute}=")
      alias_method "old_#{attribute}=", "#{attribute}="

      define_method "#{attribute}=" do |value|
        normalized_value = self.send(:"normalize_#{attribute}", value)
        self.send("old_#{attribute}=", normalized_value)
      end
    else
      define_method "#{attribute}=" do |value|
        super(self.send(:"normalize_#{attribute}", value))
      end
    end

  end
end

#normalize_default_attributesObject



69
70
71
72
73
# File 'lib/attribute_normalizer/model_inclusions.rb', line 69

def normalize_default_attributes
  AttributeNormalizer.configuration.default_attributes.each do |attribute_name, options|
    normalize_attribute(attribute_name, options) if self.column_names.include?(attribute_name)
  end
end