module Normalizy::Extension::ClassMethods

Attributes

normalizy_rules[RW]

Public Instance Methods

normalizy(*args, &block) click to toggle source
Calls superclass method
# File lib/normalizy/extensions.rb, line 76
def normalizy(*args, &block)
  options = args.extract_options!
  rules   = options[:with]

  self.normalizy_rules ||= {}

  args.each do |field|
    normalizy_rules[field] ||= []
    normalizy_rules[field] << { block: block, options: options.except(:with), rules: rules }
  end

  prepend Module.new {
    args.each do |attribute|
      define_method :"#{attribute}=" do |value|
        result = normalizy!(
          attribute: attribute,
          block:     block,
          options:   options.except(:with),
          rules:     rules,
          value:     value
        )

        if rules.is_a?(Hash) && rules.dig(:slug, :to).present?
          write_attribute rules.dig(:slug, :to), result

          super value
        else
          super result
        end
      end
    end
  }
end