mascherari 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +6 -0
- data/lib/mascherari/attr_masked.rb +24 -0
- data/lib/mascherari/formatter.rb +64 -0
- data/lib/mascherari/version.rb +3 -0
- data/lib/mascherari.rb +9 -0
- data/mascherari.gemspec +22 -0
- data/spec/attr_masked_spec.rb +75 -0
- data/spec/formatter_spec.rb +67 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/base_classes.rb +7 -0
- metadata +106 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 7ea5aa679829edcdf629788b7d7d2ac99299dfae
|
|
4
|
+
data.tar.gz: b32c17a9be5d43b8d86d383dff3b87681f082e8e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4db1d0523ee705215fe12be85bf70824e5cfbcabfe6350f9fcec241948f1a4ddc6f29766b9d3950fa2fa1e38bcbcb011d297b04decac3ee10e4972f086dc50cf
|
|
7
|
+
data.tar.gz: 693d0f852d119e1c58872959974ecd4d292a952a533d39a26d911442a673479cbd82a46dd8935aad598a7e0a117eb587e40995a7e95bffacefb4fe5d24594bd9
|
data/.gitignore
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Robson Marques
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Mascherari
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/robsonmarques/mascherari)
|
|
4
|
+
[](https://codeclimate.com/github/robsonmarques/mascherari)
|
|
5
|
+
|
|
6
|
+
An easy way to handle masks. (Work in progress)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Simple as:
|
|
11
|
+
|
|
12
|
+
$ gem install mascherari
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
To create masks for attributes, include Mascherari and set the format:
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
class Person
|
|
20
|
+
include Mascherari
|
|
21
|
+
|
|
22
|
+
attr_accessor :phone, :mobile
|
|
23
|
+
|
|
24
|
+
attr_masked :phone, :mobile, :format => "(##) ####-####"
|
|
25
|
+
end
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
That will give you two helpers for each attribute:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
# person.phone = "5554212035"
|
|
32
|
+
# person.mobile = "5599213035"
|
|
33
|
+
person.phone_masked
|
|
34
|
+
=> "(55) 5421-2035"
|
|
35
|
+
person.mobile_masked
|
|
36
|
+
=> "(55) 9921-3035"
|
|
37
|
+
|
|
38
|
+
# person.phone = "(55) 5421-2035"
|
|
39
|
+
# person.mobile = "(55) 9921-3035"
|
|
40
|
+
person.phone_unmasked
|
|
41
|
+
=> "5554212035"
|
|
42
|
+
person.mobile_unmasked
|
|
43
|
+
=> "5599213035"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Rails
|
|
47
|
+
|
|
48
|
+
Add this line to your application's Gemfile:
|
|
49
|
+
|
|
50
|
+
gem 'mascherari'
|
|
51
|
+
|
|
52
|
+
And then execute:
|
|
53
|
+
|
|
54
|
+
$ bundle
|
|
55
|
+
|
|
56
|
+
You can include Mascherari in Rails models as:
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
# config/initializers/mascherari.rb
|
|
60
|
+
ActiveSupport.on_load :active_record do
|
|
61
|
+
include Mascherari
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# app/models/person.rb
|
|
65
|
+
class Person < ActiveRecord::Base
|
|
66
|
+
attr_masked :phone, :format => "(##) ####-####"
|
|
67
|
+
end
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Contributing
|
|
71
|
+
|
|
72
|
+
1. Fork it
|
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
76
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Mascherari
|
|
2
|
+
module AttrMasked
|
|
3
|
+
def attr_masked(*attrs, options)
|
|
4
|
+
attrs.each do |attr|
|
|
5
|
+
formatter = "#{attr}_format"
|
|
6
|
+
masked = "#{attr}_masked"
|
|
7
|
+
unmasked = "#{attr}_unmasked"
|
|
8
|
+
ivar = "@#{formatter}"
|
|
9
|
+
|
|
10
|
+
define_method formatter do
|
|
11
|
+
instance_variable_get(ivar) || instance_variable_set(ivar, Formatter.new(options))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
define_method masked do
|
|
15
|
+
send(formatter).mask send(attr)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
define_method unmasked do
|
|
19
|
+
send(formatter).unmask send(attr)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module Mascherari
|
|
2
|
+
class Formatter
|
|
3
|
+
attr_reader :format, :wildcard
|
|
4
|
+
|
|
5
|
+
def initialize(options = {})
|
|
6
|
+
@format = options.fetch :format
|
|
7
|
+
@wildcard = options.fetch :wildcard, "#"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def mask(raw_value)
|
|
11
|
+
prepare_value raw_value
|
|
12
|
+
|
|
13
|
+
formatted? ? value : add_mask
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def unmask(raw_value)
|
|
17
|
+
prepare_value raw_value
|
|
18
|
+
|
|
19
|
+
formatted? ? remove_mask : value
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
protected
|
|
23
|
+
|
|
24
|
+
attr_reader :value
|
|
25
|
+
|
|
26
|
+
def prepare_value(raw_value)
|
|
27
|
+
@value = raw_value.to_s
|
|
28
|
+
|
|
29
|
+
valid_value?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def add_mask
|
|
33
|
+
value_chars = value.chars
|
|
34
|
+
|
|
35
|
+
format_chars do |char|
|
|
36
|
+
wildcard?(char) ? value_chars.shift : char
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def remove_mask
|
|
41
|
+
format_chars do |char, index|
|
|
42
|
+
value[index] if wildcard?(char)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def format_chars(&block)
|
|
47
|
+
format.chars.map.with_index { |char, index| yield char, index }.join
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def formatted?
|
|
51
|
+
value.size == format.size
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def wildcard?(char)
|
|
55
|
+
char == wildcard
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def valid_value?
|
|
59
|
+
unless formatted? || value.size == format.scan(wildcard).size
|
|
60
|
+
raise ArgumentError, "Value size don't match format"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/mascherari.rb
ADDED
data/mascherari.gemspec
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require 'mascherari/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'mascherari'
|
|
7
|
+
spec.version = Mascherari::VERSION
|
|
8
|
+
spec.authors = ['Robson Marques']
|
|
9
|
+
spec.email = ['robsonmarques@gmail.com']
|
|
10
|
+
spec.summary = 'An easy way to handle masks.'
|
|
11
|
+
spec.homepage = 'http://github.com/robsonmarques/mascherari'
|
|
12
|
+
spec.license = 'MIT'
|
|
13
|
+
|
|
14
|
+
spec.files = `git ls-files`.split($/)
|
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
17
|
+
spec.require_paths = ['lib']
|
|
18
|
+
|
|
19
|
+
spec.add_development_dependency 'rake'
|
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
|
21
|
+
spec.add_development_dependency 'rspec', '~> 2.12'
|
|
22
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Mascherari::AttrMasked do
|
|
4
|
+
subject do
|
|
5
|
+
Person.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
context 'when attribute comes without mask' do
|
|
9
|
+
before do
|
|
10
|
+
subject.phone = "1234567890"
|
|
11
|
+
subject.mobile = "0987654321"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe '#attribute_masked' do
|
|
15
|
+
it 'returns attribute with mask' do
|
|
16
|
+
expect(subject.phone_masked).to eq "(12) 3456-7890"
|
|
17
|
+
expect(subject.mobile_masked).to eq "(09) 8765-4321"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe '#attribute_unmasked' do
|
|
22
|
+
it 'returns original attribute' do
|
|
23
|
+
expect(subject.phone_unmasked).to eq "1234567890"
|
|
24
|
+
expect(subject.mobile_unmasked).to eq "0987654321"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context 'when attribute comes with mask' do
|
|
30
|
+
before do
|
|
31
|
+
subject.phone = "(12) 3456-7890"
|
|
32
|
+
subject.mobile = "(09) 8765-4321"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe '#attribute_masked' do
|
|
36
|
+
it 'returns original attribute' do
|
|
37
|
+
expect(subject.phone_masked).to eq "(12) 3456-7890"
|
|
38
|
+
expect(subject.mobile_masked).to eq "(09) 8765-4321"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe '#attribute_unmasked' do
|
|
43
|
+
it 'returns attribute without mask' do
|
|
44
|
+
expect(subject.phone_unmasked).to eq "1234567890"
|
|
45
|
+
expect(subject.mobile_unmasked).to eq "0987654321"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context 'when attribute value changes' do
|
|
51
|
+
describe '#attribute_masked' do
|
|
52
|
+
it 'masks current value' do
|
|
53
|
+
subject.phone = "1234567890"
|
|
54
|
+
|
|
55
|
+
expect(subject.phone_masked).to eq "(12) 3456-7890"
|
|
56
|
+
|
|
57
|
+
subject.phone = "0987654321"
|
|
58
|
+
|
|
59
|
+
expect(subject.phone_masked).to eq "(09) 8765-4321"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
describe '#attribute_unmasked' do
|
|
64
|
+
it 'unmasks current value' do
|
|
65
|
+
subject.phone = "(12) 3456-7890"
|
|
66
|
+
|
|
67
|
+
expect(subject.phone_unmasked).to eq "1234567890"
|
|
68
|
+
|
|
69
|
+
subject.phone = "(09) 8765-4321"
|
|
70
|
+
|
|
71
|
+
expect(subject.phone_unmasked).to eq "0987654321"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Mascherari::Formatter do
|
|
4
|
+
subject do
|
|
5
|
+
described_class.new :format => "(##) ####-####"
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe '#mask' do
|
|
9
|
+
context 'when value is unmasked' do
|
|
10
|
+
it 'returns value with mask' do
|
|
11
|
+
expect(subject.mask "1234567890").to eq "(12) 3456-7890"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
context 'when value is already masked' do
|
|
16
|
+
it 'returns value' do
|
|
17
|
+
expect(subject.mask "(12) 3456-7890").to eq "(12) 3456-7890"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
context 'when value size is invalid' do
|
|
22
|
+
it 'raises an error' do
|
|
23
|
+
expect { subject.mask "23456789" }.to raise_error "Value size don't match format"
|
|
24
|
+
expect { subject.mask "91234567890" }.to raise_error "Value size don't match format"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe '#unmask' do
|
|
30
|
+
context 'when value is masked' do
|
|
31
|
+
it 'returns value with mask' do
|
|
32
|
+
expect(subject.unmask "(12) 3456-7890").to eq "1234567890"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
context 'when value is already unmasked' do
|
|
37
|
+
it 'returns value' do
|
|
38
|
+
expect(subject.unmask "1234567890").to eq "1234567890"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context 'when value size is invalid' do
|
|
43
|
+
it 'raises an error' do
|
|
44
|
+
expect { subject.unmask "(23) 456-789" }.to raise_error "Value size don't match format"
|
|
45
|
+
expect { subject.unmask "(123) 4456-7789" }.to raise_error "Value size don't match format"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context 'when using different wildcard' do
|
|
51
|
+
subject do
|
|
52
|
+
described_class.new :format => "(**) ****-****", :wildcard => "*"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
describe '#mask' do
|
|
56
|
+
it 'returns value with mask' do
|
|
57
|
+
expect(subject.mask "1234567890").to eq "(12) 3456-7890"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
describe '#unmask' do
|
|
62
|
+
it 'returns value without mask' do
|
|
63
|
+
expect(subject.unmask "(12) 3456-7890").to eq "1234567890"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mascherari
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Robson Marques
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-10-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '>='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.3'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.3'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ~>
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '2.12'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ~>
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '2.12'
|
|
55
|
+
description:
|
|
56
|
+
email:
|
|
57
|
+
- robsonmarques@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- .gitignore
|
|
63
|
+
- .rspec
|
|
64
|
+
- .travis.yml
|
|
65
|
+
- Gemfile
|
|
66
|
+
- LICENSE.txt
|
|
67
|
+
- README.md
|
|
68
|
+
- Rakefile
|
|
69
|
+
- lib/mascherari.rb
|
|
70
|
+
- lib/mascherari/attr_masked.rb
|
|
71
|
+
- lib/mascherari/formatter.rb
|
|
72
|
+
- lib/mascherari/version.rb
|
|
73
|
+
- mascherari.gemspec
|
|
74
|
+
- spec/attr_masked_spec.rb
|
|
75
|
+
- spec/formatter_spec.rb
|
|
76
|
+
- spec/spec_helper.rb
|
|
77
|
+
- spec/support/base_classes.rb
|
|
78
|
+
homepage: http://github.com/robsonmarques/mascherari
|
|
79
|
+
licenses:
|
|
80
|
+
- MIT
|
|
81
|
+
metadata: {}
|
|
82
|
+
post_install_message:
|
|
83
|
+
rdoc_options: []
|
|
84
|
+
require_paths:
|
|
85
|
+
- lib
|
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - '>='
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - '>='
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
requirements: []
|
|
97
|
+
rubyforge_project:
|
|
98
|
+
rubygems_version: 2.1.9
|
|
99
|
+
signing_key:
|
|
100
|
+
specification_version: 4
|
|
101
|
+
summary: An easy way to handle masks.
|
|
102
|
+
test_files:
|
|
103
|
+
- spec/attr_masked_spec.rb
|
|
104
|
+
- spec/formatter_spec.rb
|
|
105
|
+
- spec/spec_helper.rb
|
|
106
|
+
- spec/support/base_classes.rb
|