Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes with zero dependencies.
AttrFilters brings simple DSL for adding filters to your PORO attributes.
Easy to integrate with Rails validation.
- Ruby >= 2.4
Add this line to your application's Gemfile:
gem 'attr_filters'
And then execute:
$ bundle
Or install it yourself as:
$ gem install attr_filters
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)>
Order of specified filters MATTERS!
Just look at some examples below:
class SignupForm
attr_accessor :name
filters :name, letters_only: true, trim: true
end
form = SingupForm.new
form.name = " Mike 123 "
form.filter!
form.name # => "Mike"
Now if we will change filters order we will have different result:
class SignupForm
attr_accessor :name
filters :name, trim: true, letters_only: true
end
form = SingupForm.new
form.name = " Mike 123 "
form.filter!
form.name # => "Mike "
In the second example the trim
filter runs first removing leading and trailing spaces, and then the letters_only
filter removes the "123"
substring.
- 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"
trim
- removes leading and trailing whitespacesdowncase
- replaces all upcase letters to lowercase onesupcase
- replaces all lowercase letters to upcase onescapitalize
- upcases first letter and lowercase otherssqueeze
- removes duplicating whitespacesnumbers_only
- removes non digits charactersletters_only
- removes non letter characters, acceptexcept
options asfilters first_name: { except: %w[' -] }
date
- parse string to date using specified format. Default format is%Y-%m-%d
.
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.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the AttrFilters project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.