CNS Brazil
- CNS (Cartão Nacional de Saúde) - CNS is a national health card
- Is a validator and generator based in algorithm from https://integracao.esusab.ufsc.br/v211/docs/algoritmo_CNS.html
Usage
Installing
Add this line to your application's Gemfile:
gem 'cns_brazil'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cns_brazil
Validator
ActiveRecord
- Just use as any other validator:
class User < ActiveRecord::Base
validates :cns, '::CnsBrazil::Cns': true
end
- Use with anny column name
class User < ActiveRecord::Base
validates :my_cns, '::CnsBrazil::Cns': true
end
- Allow blank value
class User < ActiveRecord::Base
validates :cns, '::CnsBrazil::Cns': true, allow_blank: true
end
Elsewhere
require "cns_brazil"
cns_validator = CnsBrazil::Cns.new(value: '1234567891234516')
cns_validator.valid? # true || false
Generator
require "cns_brazil"
CnsBrazil::Cns.generate # returns a valid CNS
Test
Obs: If your project has shoulda-matchers gem you can use next matcher
Create a file spec/support/cns_brazil.rb
and add next content
require 'cns_brazil/validate_cns_matcher'
Use in your tests:
specify { is_expected.to validate_cns } # It will test the attribute :cns by default
specify { is_expected.to validate_cns(:my_cns) }
With blank value
specify { is_expected.to validate_cns.allow_blank }
specify { is_expected.to validate_cns(:my_cns).allow_blank }
Usage example
Contributing
- Run using Docker:
docker-compose run web bash
- In bash Run:
bundle install
- Run spec:
spec
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request