Sha256: 89c2b1592403481e948c6eecb7de22e911603ce5337b3e10e5c8a468c29ef0bf

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module ActiveCleaner
  # Defines the DSL methods available in your model.
  module HelperMethods

    # Registers a cleaner to a bunch of fields by its name.
    #
    # === Options
    #
    # [:as]
    #   the kind of the cleaner. Default to +:string+
    #
    # Extra options are passed to the cleaner.
    #
    # === Example
    #
    #   class MyModel
    #     include ActiveCleaner
    #
    #     clean :name, nilify: false
    #     clean :firstname, :lastname, nilify: false
    #     clean :resume, as: :markdown
    #   end
    def clean(*attr_names)
      options = attr_names.extract_options!.symbolize_keys
      attr_names.flatten!

      options[:as] ||= :string

      cleaner = "active_cleaner/#{options.delete(:as)}_cleaner".camelize.constantize

      attr_names.each do |attr_name|
        clean_with cleaner.new(attr_name, options)
      end
    end

    # Registers a cleaner by an instance of it.
    #
    #   class MyModel
    #     include ActiveCleaner
    #
    #     clean_with ActiveCleaner::StringCleaner.new(:name, nilify: false)
    #   end
    def clean_with(cleaner)
      _cleaners[cleaner.attr_name] << cleaner
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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