AttrFilters

Build Status Gem Version FOSSA Status

Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes with zero dependencies.

Description

AttrFilters brings simple DSL for adding filters to your PORO attributes.
Easy to integrate with Rails validation.

Requirements

  • Ruby >= 2.4

Installation

Add this line to your application's Gemfile:

gem 'attr_filters'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attr_filters

Usage

You can add filters like that:

class SingupForm
  include AttrFilters

  attr_accessor :email, :first_name, :last_name, :zip, :birth_date, :due_date

  filters :email, trim: true, downcase: true
  filters :first_name, :last_name, trim: true, letters_only: true
  filters :zip, trim: true, numbers_only: true
  filters :birth_date, date: true
  filters :due_date, date: "%m-%d-%Y"
end

And call #filter! method to apply filters to attributes values

form = SingupForm.new

form.email      = "  [email protected] "
form.first_name = "Mike 123"
form.last_name  = " Dou"
form.zip        = "abc12345"
form.birth_date = "2019-11-02"
form.due_date   = "11-02-2019"

form.filter!

form.email        # => "[email protected]"
form.first_name   # => "Mike"
form.last_name    # => "Dou"
form.zip          # => "12345"
form.birth_date   # => #<Date: 2019-11-02 ((2458790j,0s,0n),+0s,2299161j)>
form.due_date     # => #<Date: 2019-11-02 ((2458790j,0s,0n),+0s,2299161j)>

Integration with Rails

Requirements

  • ActiveModel >= 4.2.0

For Rails integration you should include AttrFilters::ActiveModel.
After that you can add filters using validates method.

class SingupForm
  include ActiveModel::Model
  include AttrFilters::ActiveModel

  attr_accessor :user_name

  validates :user_name, presence: true, filters: { trim: true, squeeze: true }
end

form = SingupForm.new(user_name: "  Mike   Dou  ")
form.filter!
form.user_name  # => "Mike Dou"

Also filters can be run automatically before validation. Require ActiveModel::Validations::Callbacks

class SingupForm
  include ActiveModel::Model
  include ActiveModel::Validations::Callbacks
  include AttrFilters::ActiveModel

  attr_accessor :user_name, :zip

  validates :user_name, presence: true, filters: { trim: true, squeeze: true }
  filters :zip, trim: true, numbers_only: true
end

form = SingupForm.new(user_name: "  Mike   Dou  ", zip: "12345abc")
form.valid?
form.user_name  # => "Mike Dou"
form.zip        # => "12345"

Available Filters

  • trim - removes leading and trailing whitespaces
  • downcase - replaces all upcase letters to lowercase ones
  • upcase - replaces all lowercase letters to upcase ones
  • capitalize - upcases first letter and lowercase others
  • squeeze - removes duplicating whitespaces
  • numbers_only - removes non digits characters
  • letters_only - removes non letter characters, accept except options as filters first_name: { except: %w[' -] }
  • date - parse string to date using specified format. Default format is %Y-%m-%d.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Syndicode/attr_filters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the AttrFilters project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Authors

Syndicode.com