maskable_attribute 0.0.3 → 0.0.5
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.
- data/Gemfile.lock +1 -1
- data/lib/maskable_attribute/acts_as_maskable_attribute.rb +9 -6
- data/lib/maskable_attribute/formatting.rb +10 -6
- data/lib/maskable_attribute/mask.rb +1 -1
- data/lib/maskable_attribute/maskable_attribute.rb +3 -2
- data/lib/maskable_attribute/version.rb +1 -1
- data/test/formatting_test.rb +8 -0
- data/test/maskable_attribute_test.rb +31 -0
- metadata +10 -5
data/Gemfile.lock
CHANGED
|
@@ -12,13 +12,16 @@ module MaskableAttribute
|
|
|
12
12
|
# ==== Examples
|
|
13
13
|
#
|
|
14
14
|
# class Foo < ActiveRecord::Base
|
|
15
|
-
# maskable_attrribute :some_attribute,
|
|
15
|
+
# maskable_attrribute :some_attribute,
|
|
16
|
+
# [ :some_method_be_used_as_a_mask, :another_attribute_mask ],
|
|
17
|
+
# :protected_prefixes => [ 'prefix' ]
|
|
16
18
|
# end
|
|
17
19
|
|
|
18
|
-
def maskable_attribute(attribute_to_mask, masks)
|
|
20
|
+
def maskable_attribute(attribute_to_mask, masks, options = {})
|
|
19
21
|
raise ArgumentError, "invalid argument (expected attribute)" unless column_names.include? attribute_to_mask.to_s
|
|
20
22
|
|
|
21
23
|
cattr_accessor :masks
|
|
24
|
+
|
|
22
25
|
self.masks ||= {}
|
|
23
26
|
self.masks[attribute_to_mask] = masks
|
|
24
27
|
self.masks
|
|
@@ -28,19 +31,19 @@ module MaskableAttribute
|
|
|
28
31
|
end
|
|
29
32
|
|
|
30
33
|
define_method "#{attribute_to_mask}=" do |value|
|
|
31
|
-
write_attribute attribute_to_mask, masked_attribute(attribute_to_mask).set(value)
|
|
34
|
+
write_attribute attribute_to_mask, masked_attribute(attribute_to_mask, options).set(value)
|
|
32
35
|
end
|
|
33
36
|
|
|
34
37
|
define_method "maskable_#{attribute_to_mask}" do
|
|
35
|
-
masked_attribute attribute_to_mask
|
|
38
|
+
masked_attribute attribute_to_mask, options
|
|
36
39
|
end
|
|
37
40
|
end
|
|
38
41
|
end
|
|
39
42
|
|
|
40
43
|
attr_accessor :masked_attribute
|
|
41
44
|
|
|
42
|
-
def masked_attribute(attribute)
|
|
43
|
-
@masked_attribute ||= MaskableAttribute.new self, attribute, self.class.masks[attribute]
|
|
45
|
+
def masked_attribute(attribute, options)
|
|
46
|
+
@masked_attribute ||= MaskableAttribute.new self, attribute, self.class.masks[attribute], options
|
|
44
47
|
end
|
|
45
48
|
end
|
|
46
49
|
end
|
|
@@ -58,12 +58,16 @@ module MaskableAttribute
|
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
def apply(input)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
@method.
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
begin
|
|
62
|
+
if @method.is_a? Symbol
|
|
63
|
+
input.send(@method)
|
|
64
|
+
elsif @method.is_a? Proc
|
|
65
|
+
@method.call input
|
|
66
|
+
else
|
|
67
|
+
input
|
|
68
|
+
end
|
|
69
|
+
rescue
|
|
70
|
+
nil
|
|
67
71
|
end
|
|
68
72
|
end
|
|
69
73
|
end
|
|
@@ -87,7 +87,7 @@ module MaskableAttribute
|
|
|
87
87
|
def unmask(*args)
|
|
88
88
|
object = args.first
|
|
89
89
|
options = args.extract_options!
|
|
90
|
-
format = options[:formatted] || accessor.sub("_" + name.to_s, "").strip.to_sym
|
|
90
|
+
format = (options[:formatted] || accessor).to_s.sub("_" + name.to_s, "").strip.to_sym
|
|
91
91
|
|
|
92
92
|
formats.apply format do
|
|
93
93
|
begin
|
|
@@ -2,10 +2,11 @@ module MaskableAttribute
|
|
|
2
2
|
class MaskableAttribute
|
|
3
3
|
attr_accessor :object, :attribute, :masks
|
|
4
4
|
|
|
5
|
-
def initialize(object, attribute, masks)
|
|
5
|
+
def initialize(object, attribute, masks, options)
|
|
6
6
|
@object = object
|
|
7
7
|
@attribute = attribute
|
|
8
8
|
@masks = Masks.new masks
|
|
9
|
+
@protected_prefixes = Array.wrap(options.delete(:protected_prefixes) || options.delete(:protected_prefix)).join('|')
|
|
9
10
|
end
|
|
10
11
|
|
|
11
12
|
def masks
|
|
@@ -37,7 +38,7 @@ module MaskableAttribute
|
|
|
37
38
|
unless value.blank?
|
|
38
39
|
@masks.each do |mask|
|
|
39
40
|
mask.accessed_by.each do |mask_accessor|
|
|
40
|
-
value.sub! /#{mask.unmask(@object, :formatted => mask_accessor)}(?![^{]*})/, "{#{mask_accessor}}" unless mask.unmask(@object).blank?
|
|
41
|
+
value.sub! /#{"(?<!#{@protected_prefixes})" unless @protected_prefixes.blank?}#{mask.unmask(@object, :formatted => mask_accessor)}(?![^{]*})/, "{#{mask_accessor}}" unless mask.unmask(@object).blank?
|
|
41
42
|
end
|
|
42
43
|
end
|
|
43
44
|
end
|
data/test/formatting_test.rb
CHANGED
|
@@ -13,6 +13,14 @@ class FormattingTest < ActiveSupport::TestCase
|
|
|
13
13
|
assert_equal "SUCCESS", format.apply("success")
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
test "should return nil when formatting methods raise exceptions" do
|
|
17
|
+
format = MaskableAttribute::Formatting::Format.new Proc.new { |value| raise 'I meant to do this...' }
|
|
18
|
+
|
|
19
|
+
assert_nothing_raised do
|
|
20
|
+
assert_nil format.apply("success")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
16
24
|
#describe Formats
|
|
17
25
|
test "should accept a hash of a single format" do
|
|
18
26
|
formats = MaskableAttribute::Formatting::Formats.new :formats => :upcase
|
|
@@ -174,4 +174,35 @@ class MaskableAttributeTest < ActiveSupport::TestCase
|
|
|
174
174
|
|
|
175
175
|
assert_equal "02", @hickwell.bar, "Did not retrieve mask having a proc and a format specified"
|
|
176
176
|
end
|
|
177
|
+
|
|
178
|
+
test "should not confuse masks' formatting" do
|
|
179
|
+
class Pickwell < Hickwell
|
|
180
|
+
maskable_attribute :bar, { :foo => {
|
|
181
|
+
:method => Proc.new { "2" },
|
|
182
|
+
:format => { :two_digit => Proc.new { |value| format "%02d", value } }
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
@pickwell = Pickwell.create! :bar => "{foo}"
|
|
188
|
+
|
|
189
|
+
assert_equal "2", @pickwell.bar
|
|
190
|
+
@pickwell.bar = "2"
|
|
191
|
+
assert_equal "{foo}", @pickwell.maskable_bar.unmasked
|
|
192
|
+
@pickwell.bar = "02"
|
|
193
|
+
assert_equal "{two_digit_foo}", @pickwell.maskable_bar.unmasked
|
|
194
|
+
assert_equal "02", @pickwell.bar
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
test "should not mask if specified as a non-maskable prefix" do
|
|
198
|
+
class Zickwell < Hickwell
|
|
199
|
+
maskable_attribute :bar, { :foo => { :method => Proc.new { "TEST" } } }, { :protected_prefixes => [ "prefix" ] }
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
@zickwell = Zickwell.create! :bar => "TEST"
|
|
203
|
+
assert_equal "{foo}", @zickwell.maskable_bar.unmasked, "Mask not being replaced"
|
|
204
|
+
|
|
205
|
+
@zickwell.update_attribute :bar, "prefixTEST"
|
|
206
|
+
assert_equal "prefixTEST", @zickwell.maskable_bar.unmasked, "Incorrectly masked for non-maskable prefix"
|
|
207
|
+
end
|
|
177
208
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: maskable_attribute
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.5
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,11 +9,11 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-07-
|
|
12
|
+
date: 2012-07-23 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rails
|
|
16
|
-
requirement:
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>='
|
|
@@ -21,7 +21,12 @@ dependencies:
|
|
|
21
21
|
version: 2.3.10
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements:
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 2.3.10
|
|
25
30
|
description: A maskable attribute is an attribute that is made up of other attributes
|
|
26
31
|
(masks), this ordering is set in the masked attribute and preserved across updates.
|
|
27
32
|
email:
|
|
@@ -116,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
116
121
|
version: '0'
|
|
117
122
|
requirements: []
|
|
118
123
|
rubyforge_project: maskable_attribute
|
|
119
|
-
rubygems_version: 1.8.
|
|
124
|
+
rubygems_version: 1.8.24
|
|
120
125
|
signing_key:
|
|
121
126
|
specification_version: 3
|
|
122
127
|
summary: Allows Ruby on Rails to have a maskable attribute.
|