An easy way to handle masks. (Work in progress)
Simple as:
$ gem install mascherari
To create masks for attributes, include Mascherari and set the format:
class Person
include Mascherari
attr_accessor :phone
attr_masked :phone, :format => "(##) ####-####"
end
That will give you two helpers to use along with the object:
# person.phone = "5554212035"
person.phone_masked
=> "(55) 5421-2035"
# person.phone = "(55) 5421-2035"
person.phone_unmasked
=> "5554212035"
You can set a format for more than one attribute:
attr_masked :phone, :mobile, :format => "(##) ####-####"
And also use a different wildcard, if needed:
attr_masked :phone, :format => "(**) ****-****", :wildcard => "*"
For the cases when format can vary, it's possible to set multiple formats:
attr_masked :phone, :format => ["(##) ####-####", "(###) ####-####"]
Add this line to your application's Gemfile:
gem 'mascherari'
And then execute:
$ bundle
You can include Mascherari in Rails models as:
# config/initializers/mascherari.rb
ActiveSupport.on_load :active_record do
include Mascherari
end
# app/models/person.rb
class Person < ActiveRecord::Base
attr_masked :phone, :format => "(##) ####-####"
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request