Module: Normalize::ClassMethods

Defined in:
lib/normalize.rb

Instance Method Summary collapse

Instance Method Details

#extract_normalizer(options_or_normalizer) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/normalize.rb', line 28

def extract_normalizer(options_or_normalizer)
  if options_or_normalizer.respond_to?(:[])
    options_or_normalizer[:with]
  else
    options_or_normalizer
  end
end

#normalize(attribute, options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/normalize.rb', line 11

def normalize(attribute, options)
  normalizer = extract_normalizer(options)
  
  class_eval do
    if method_defined? "#{attribute}="
      define_method "#{attribute}_with_normalizer=" do |value|
        send("#{attribute}_without_normalizer=", normalize_value(value, normalizer))
      end
      alias_method_chain "#{attribute}=", :normalizer
    else
      define_method "#{attribute}=" do |value|
        super(normalize_value(value, normalizer))
      end
    end
  end
end