lib/active_cleaner.rb in activecleaner-0.3.1 vs lib/active_cleaner.rb in activecleaner-0.3.2
- old
+ new
@@ -1,64 +1,84 @@
-# encoding: utf-8
+# frozen_string_literal: true
-require 'active_support'
-#require 'active_support/rails'
-require 'active_model'
+require "active_support"
+require "active_model"
-require 'active_cleaner/helper_methods'
+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/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'
+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
+ define_callbacks :cleaning, scope: :name
class_attribute :_cleaners
- self._cleaners = Hash.new { |h,k| h[k] = [] }
+ self._cleaners = Hash.new { |h, k| h[k] = [] }
set_callback :validate, :before, :run_cleaners!
+ end
- end # included
+ module ClassMethods #:nodoc:
-
- module ClassMethods
-
def inherited(base) #:nodoc:
dup = _cleaners.dup
- base._cleaners = dup.each { |k, v| dup[k] = v.dup }
+ base._cleaners =
+ dup.each do |attr_name, cleaner|
+ dup[attr_name] = cleaner.dup
+ end
super
end
- end # ClassMethods
+ end
-
+ # Do run the cleaners
def run_cleaners!
- self._cleaners.each do |attr_name, 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