mascherari 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +22 -9
- data/lib/mascherari/attr_masked.rb +11 -14
- data/lib/mascherari/formatter.rb +28 -20
- data/lib/mascherari/version.rb +1 -1
- data/spec/attr_masked_spec.rb +27 -0
- data/spec/support/base_classes.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 01c3c81144ed2c7555d9ddf5c58fcadb50186e0b
|
|
4
|
+
data.tar.gz: 659c760fdd7f93aae15242b1168f26242da43d49
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c4e3a1d33f3e0afa8100afe069cf9bce12d69bf45b8540e079f6816f139aa84e0e453568e390b714d8d657de471ac9d71521ca387f35c9753259b2219389c6c7
|
|
7
|
+
data.tar.gz: cc5ed1b2d04dd5bc54b10a941473f5bb943a4ea4faaf21ba51e4648186b18e81ccff9d416560d6fe06e800876fd77ac902dc9b49e0601f27f66a3362f5e24de5
|
data/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# Mascherari
|
|
2
2
|
|
|
3
|
+
[](https://badge.fury.io/rb/mascherari)
|
|
3
4
|
[](https://travis-ci.org/robsonmarques/mascherari)
|
|
4
5
|
[](https://codeclimate.com/github/robsonmarques/mascherari)
|
|
5
6
|
|
|
@@ -19,28 +20,40 @@ To create masks for attributes, include Mascherari and set the format:
|
|
|
19
20
|
class Person
|
|
20
21
|
include Mascherari
|
|
21
22
|
|
|
22
|
-
attr_accessor :phone
|
|
23
|
+
attr_accessor :phone
|
|
23
24
|
|
|
24
|
-
attr_masked :phone, :
|
|
25
|
+
attr_masked :phone, :format => "(##) ####-####"
|
|
25
26
|
end
|
|
26
27
|
```
|
|
27
28
|
|
|
28
|
-
That will give you two helpers
|
|
29
|
+
That will give you two helpers to use along with the object:
|
|
29
30
|
|
|
30
31
|
```ruby
|
|
31
32
|
# person.phone = "5554212035"
|
|
32
|
-
# person.mobile = "5599213035"
|
|
33
33
|
person.phone_masked
|
|
34
34
|
=> "(55) 5421-2035"
|
|
35
|
-
person.mobile_masked
|
|
36
|
-
=> "(55) 9921-3035"
|
|
37
35
|
|
|
38
36
|
# person.phone = "(55) 5421-2035"
|
|
39
|
-
# person.mobile = "(55) 9921-3035"
|
|
40
37
|
person.phone_unmasked
|
|
41
38
|
=> "5554212035"
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
You can set a format for more than one attribute:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
attr_masked :phone, :mobile, :format => "(##) ####-####"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
And also use a different wildcard, if needed:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
attr_masked :phone, :format => "(**) ****-****", :wildcard => "*"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
For the cases when format can vary, it's possible to set multiple formats:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
attr_masked :phone, :format => ["(##) ####-####", "(###) ####-####"]
|
|
44
57
|
```
|
|
45
58
|
|
|
46
59
|
## Rails
|
|
@@ -2,22 +2,19 @@ module Mascherari
|
|
|
2
2
|
module AttrMasked
|
|
3
3
|
def attr_masked(*attrs, options)
|
|
4
4
|
attrs.each do |attr|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
class_eval %{
|
|
6
|
+
def #{attr}_format
|
|
7
|
+
@#{attr}_format ||= Formatter.new(#{options})
|
|
8
|
+
end
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
def #{attr}_masked
|
|
11
|
+
#{attr}_format.mask #{attr}
|
|
12
|
+
end
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
define_method unmasked do
|
|
19
|
-
send(formatter).unmask send(attr)
|
|
20
|
-
end
|
|
14
|
+
def #{attr}_unmasked
|
|
15
|
+
#{attr}_format.unmask #{attr}
|
|
16
|
+
end
|
|
17
|
+
}
|
|
21
18
|
end
|
|
22
19
|
end
|
|
23
20
|
end
|
data/lib/mascherari/formatter.rb
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
module Mascherari
|
|
2
2
|
class Formatter
|
|
3
|
-
attr_reader :format, :wildcard
|
|
4
|
-
|
|
5
3
|
def initialize(options = {})
|
|
6
|
-
@
|
|
4
|
+
@format_list = Array(options.fetch :format)
|
|
7
5
|
@wildcard = options.fetch :wildcard, "#"
|
|
8
6
|
end
|
|
9
7
|
|
|
@@ -21,44 +19,54 @@ module Mascherari
|
|
|
21
19
|
|
|
22
20
|
protected
|
|
23
21
|
|
|
24
|
-
attr_reader :value
|
|
22
|
+
attr_reader :value, :format, :format_list, :wildcard
|
|
25
23
|
|
|
26
24
|
def prepare_value(raw_value)
|
|
27
25
|
@value = raw_value.to_s
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
unless find_format
|
|
28
|
+
raise ArgumentError, "Value size don't match format"
|
|
29
|
+
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
def add_mask
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
format_chars do |char|
|
|
36
|
-
wildcard?(char) ? value_chars.shift : char
|
|
37
|
-
end
|
|
33
|
+
format_sub(wildcard) { |_, index| value[index] }
|
|
38
34
|
end
|
|
39
35
|
|
|
40
36
|
def remove_mask
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
format_sub { |char, index| value[index] if wildcard?(char) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def format_sub(pattern = /./, &block)
|
|
41
|
+
format.gsub(pattern).each_with_index { |char, index| yield char, index }
|
|
44
42
|
end
|
|
45
43
|
|
|
46
|
-
def
|
|
47
|
-
|
|
44
|
+
def format_regexp
|
|
45
|
+
/\A#{format_sub { |char| format_matcher char }}\z/
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def format_matcher(char)
|
|
49
|
+
wildcard?(char) ? '\S' : "\\#{char}"
|
|
48
50
|
end
|
|
49
51
|
|
|
50
52
|
def formatted?
|
|
51
|
-
value
|
|
53
|
+
value =~ format_regexp
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def wildcard_size?
|
|
57
|
+
value.size == format.scan(wildcard).size
|
|
52
58
|
end
|
|
53
59
|
|
|
54
60
|
def wildcard?(char)
|
|
55
61
|
char == wildcard
|
|
56
62
|
end
|
|
57
63
|
|
|
58
|
-
def
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
def find_format
|
|
65
|
+
format_for { formatted? } || format_for { wildcard_size? }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def format_for(&block)
|
|
69
|
+
format_list.any? { |format| @format = format; yield }
|
|
62
70
|
end
|
|
63
71
|
end
|
|
64
72
|
end
|
data/lib/mascherari/version.rb
CHANGED
data/spec/attr_masked_spec.rb
CHANGED
|
@@ -72,4 +72,31 @@ describe Mascherari::AttrMasked do
|
|
|
72
72
|
end
|
|
73
73
|
end
|
|
74
74
|
end
|
|
75
|
+
|
|
76
|
+
context 'when have multiple formats' do
|
|
77
|
+
describe '#attribute_masked' do
|
|
78
|
+
it 'masks using right format' do
|
|
79
|
+
subject.account = "1234567"
|
|
80
|
+
|
|
81
|
+
expect(subject.account_masked).to eq "12.345-67"
|
|
82
|
+
|
|
83
|
+
subject.account = "123456789"
|
|
84
|
+
|
|
85
|
+
expect(subject.account_masked).to eq "123.456.789"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
describe '#attribute_unmasked' do
|
|
90
|
+
it 'unmasks using right format' do
|
|
91
|
+
subject.account = "12.345-67"
|
|
92
|
+
|
|
93
|
+
expect(subject.account_unmasked).to eq "1234567"
|
|
94
|
+
|
|
95
|
+
subject.account = "123.456.789"
|
|
96
|
+
|
|
97
|
+
expect(subject.account_unmasked).to eq "123456789"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
end
|
|
75
102
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mascherari
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robson Marques
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2013-
|
|
11
|
+
date: 2013-11-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|