Light weight gem for filtering PORO attributes with zero dependencies.
AttrFilters brings simple DSL for adding filters to your PORO attributes.
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
filters :email, trim: true, downcase: true
filters :first_name, :last_name, trim: true, letters_only: true
filters :zip, trim: true, numbers_only: true
end
And call #filter!
method to apply filters for attributes values
form = SingupForm.new
form.email = " [email protected] "
form.first_name = "Mike 123"
form.last_name = " Dou"
form.zip = "abc12345"
form.filter!
form.email # => "[email protected]"
form.first_name # => "Mike"
form.last_name # => "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
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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.