File: model_inclusions.rb

package info (click to toggle)
ruby-attribute-normalizer 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 108 kB
  • sloc: ruby: 213; makefile: 7
file content (84 lines) | stat: -rw-r--r-- 3,199 bytes parent folder | download
1
2
3
4
5
6
7
8
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module AttributeNormalizer

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    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

    alias :normalize_attribute :normalize_attributes

    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

    # def inherited(subclass)
    #   super
    #   if subclass.name.present? && subclass.respond_to?(:table_exists?) && (subclass.table_exists? rescue false)
    #     subclass.normalize_default_attributes
    #   end
    # end

  end

end