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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ea5aa679829edcdf629788b7d7d2ac99299dfae
4
- data.tar.gz: b32c17a9be5d43b8d86d383dff3b87681f082e8e
3
+ metadata.gz: 01c3c81144ed2c7555d9ddf5c58fcadb50186e0b
4
+ data.tar.gz: 659c760fdd7f93aae15242b1168f26242da43d49
5
5
  SHA512:
6
- metadata.gz: 4db1d0523ee705215fe12be85bf70824e5cfbcabfe6350f9fcec241948f1a4ddc6f29766b9d3950fa2fa1e38bcbcb011d297b04decac3ee10e4972f086dc50cf
7
- data.tar.gz: 693d0f852d119e1c58872959974ecd4d292a952a533d39a26d911442a673479cbd82a46dd8935aad598a7e0a117eb587e40995a7e95bffacefb4fe5d24594bd9
6
+ metadata.gz: c4e3a1d33f3e0afa8100afe069cf9bce12d69bf45b8540e079f6816f139aa84e0e453568e390b714d8d657de471ac9d71521ca387f35c9753259b2219389c6c7
7
+ data.tar.gz: cc5ed1b2d04dd5bc54b10a941473f5bb943a4ea4faaf21ba51e4648186b18e81ccff9d416560d6fe06e800876fd77ac902dc9b49e0601f27f66a3362f5e24de5
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Mascherari
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/mascherari.png)](https://badge.fury.io/rb/mascherari)
3
4
  [![Build Status](https://travis-ci.org/robsonmarques/mascherari.png)](https://travis-ci.org/robsonmarques/mascherari)
4
5
  [![Code Climate](https://codeclimate.com/github/robsonmarques/mascherari.png)](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, :mobile
23
+ attr_accessor :phone
23
24
 
24
- attr_masked :phone, :mobile, :format => "(##) ####-####"
25
+ attr_masked :phone, :format => "(##) ####-####"
25
26
  end
26
27
  ```
27
28
 
28
- That will give you two helpers for each attribute:
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
- person.mobile_unmasked
43
- => "5599213035"
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
- formatter = "#{attr}_format"
6
- masked = "#{attr}_masked"
7
- unmasked = "#{attr}_unmasked"
8
- ivar = "@#{formatter}"
5
+ class_eval %{
6
+ def #{attr}_format
7
+ @#{attr}_format ||= Formatter.new(#{options})
8
+ end
9
9
 
10
- define_method formatter do
11
- instance_variable_get(ivar) || instance_variable_set(ivar, Formatter.new(options))
12
- end
10
+ def #{attr}_masked
11
+ #{attr}_format.mask #{attr}
12
+ end
13
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
14
+ def #{attr}_unmasked
15
+ #{attr}_format.unmask #{attr}
16
+ end
17
+ }
21
18
  end
22
19
  end
23
20
  end
@@ -1,9 +1,7 @@
1
1
  module Mascherari
2
2
  class Formatter
3
- attr_reader :format, :wildcard
4
-
5
3
  def initialize(options = {})
6
- @format = options.fetch :format
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
- valid_value?
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
- value_chars = value.chars
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
- format_chars do |char, index|
42
- value[index] if wildcard?(char)
43
- end
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 format_chars(&block)
47
- format.chars.map.with_index { |char, index| yield char, index }.join
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.size == format.size
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 valid_value?
59
- unless formatted? || value.size == format.scan(wildcard).size
60
- raise ArgumentError, "Value size don't match format"
61
- end
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
@@ -1,3 +1,3 @@
1
1
  module Mascherari
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
@@ -1,7 +1,8 @@
1
1
  class Person
2
2
  include Mascherari
3
3
 
4
- attr_accessor :phone, :mobile
4
+ attr_accessor :phone, :mobile, :account
5
5
 
6
6
  attr_masked :phone, :mobile, :format => "(##) ####-####"
7
+ attr_masked :account, :format => ["##.###-##", "###.###.###"]
7
8
  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.1
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-10-23 00:00:00.000000000 Z
11
+ date: 2013-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake