Sha256: 63c8ba720bad38b6ced382063a4864b3c47b1922d8757901c2248632848d2600

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require "active_support"
require "active_model"

require "active_cleaner/helper_methods"

require "active_cleaner/base_cleaner"
require "active_cleaner/string_cleaner"
require "active_cleaner/text_cleaner"
require "active_cleaner/markdown_cleaner"
require "active_cleaner/utf8mb3_cleaner"

require "active_cleaner/version"

# = ActiveCleaner
#
# See HelperMethods for the DSL.
#
# == Example
#
#   class Post
#     include Mongoid::Document
#     include ActiveCleaner
#
#     field :title
#     field :subtitle
#     clean :title, :subtitle, nilify: true
#
#     field :body
#     clean :body, as: :text, nilify: true
#   end
module ActiveCleaner

  extend ActiveSupport::Concern

  included do
    include ActiveModel::Validations

    extend HelperMethods
    include HelperMethods

    define_callbacks :cleaning, scope: :name

    class_attribute :_cleaners
    self._cleaners = Hash.new { |h, k| h[k] = [] }

    set_callback :validate, :before, :run_cleaners!
  end

  module ClassMethods #:nodoc:

    def inherited(base) #:nodoc:
      dup = _cleaners.dup
      base._cleaners =
        dup.each do |attr_name, cleaner|
          dup[attr_name] = cleaner.dup
        end
      super
    end

  end

  # Do run the cleaners
  def run_cleaners!
    _cleaners.each do |_attr_name, cleaners|
      cleaners.each do |cleaner|
        cleaner.clean(self)
      end
    end

    true
  end

  # Method used by the cleaners to read the value of an attribute.
  def read_attribute_for_cleaning(attr_name)
    send(attr_name)
  end

  # Method used by the cleaners to write the value of an attribute.
  def write_attribute_after_cleaning(attr_name, value)
    send(:"#{attr_name}=", value)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activecleaner-0.3.3 lib/active_cleaner.rb
activecleaner-0.3.2 lib/active_cleaner.rb