maskable_attribute 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.
|
@@ -28,10 +28,12 @@ module MaskableAttribute
|
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
def [](accessor)
|
|
31
|
+
accessor = accessor.tr " ", "_"
|
|
31
32
|
(find_by_accessor(accessor) || Mask.new).accessor(accessor)
|
|
32
33
|
end
|
|
33
34
|
|
|
34
35
|
def find_by_accessor(accessor)
|
|
36
|
+
accessor = accessor.tr " ", "_"
|
|
35
37
|
@masks.find do |mask|
|
|
36
38
|
mask.accessed_by? accessor
|
|
37
39
|
end
|
|
@@ -47,10 +49,11 @@ module MaskableAttribute
|
|
|
47
49
|
|
|
48
50
|
def initialize(options=nil)
|
|
49
51
|
if options.is_a? Symbol
|
|
50
|
-
|
|
52
|
+
self.name = (@method = options.to_sym)
|
|
51
53
|
options = {}
|
|
52
54
|
elsif options.is_a? Hash
|
|
53
|
-
|
|
55
|
+
self.name = options.flatten.first
|
|
56
|
+
options = options.flatten.last
|
|
54
57
|
if options.is_a? Symbol or options.is_a? Proc
|
|
55
58
|
@method = options
|
|
56
59
|
options = {}
|
|
@@ -59,15 +62,15 @@ module MaskableAttribute
|
|
|
59
62
|
@method = options.shift
|
|
60
63
|
options = options.extract_options!
|
|
61
64
|
else
|
|
62
|
-
@method =
|
|
65
|
+
@method = name.to_sym
|
|
63
66
|
end
|
|
64
67
|
end
|
|
65
68
|
end
|
|
66
69
|
@formats = Formatting::Formats.new options
|
|
67
70
|
end
|
|
68
71
|
|
|
69
|
-
def name
|
|
70
|
-
@name.to_s.tr
|
|
72
|
+
def name=(value)
|
|
73
|
+
@name = value.to_s.tr(' ', '_')
|
|
71
74
|
end
|
|
72
75
|
|
|
73
76
|
def accessor(*accessed_with)
|
|
@@ -82,7 +85,7 @@ module MaskableAttribute
|
|
|
82
85
|
def unmask(*args)
|
|
83
86
|
object = args.first
|
|
84
87
|
options = args.extract_options!
|
|
85
|
-
format = options[:formatted] || accessor.sub(
|
|
88
|
+
format = options[:formatted] || accessor.sub("_" + name.to_s, "").strip.to_sym
|
|
86
89
|
|
|
87
90
|
formats.apply format do
|
|
88
91
|
begin
|
|
@@ -100,13 +103,13 @@ module MaskableAttribute
|
|
|
100
103
|
end
|
|
101
104
|
|
|
102
105
|
def accessed_by
|
|
103
|
-
formats.names.map { |format| format.to_s + "
|
|
106
|
+
formats.names.map { |format| format.to_s + "_" + name}.tap do |accessed_by|
|
|
104
107
|
accessed_by.push name unless formats.are_exclusive?
|
|
105
108
|
end
|
|
106
109
|
end
|
|
107
110
|
|
|
108
111
|
def accessed_by?(possible_accessor)
|
|
109
|
-
accessed_by.include? possible_accessor
|
|
112
|
+
accessed_by.include? possible_accessor
|
|
110
113
|
end
|
|
111
114
|
end
|
|
112
115
|
end
|
|
@@ -16,7 +16,8 @@ module MaskableAttribute
|
|
|
16
16
|
value = unmasked
|
|
17
17
|
if !value.blank? and value.match(/\{.*\}/)
|
|
18
18
|
value.scan(/(?<={)[^}]+(?=})/).each do |mask| #mask: two_digit model_series
|
|
19
|
-
|
|
19
|
+
mask_value = @masks[mask].unmask(@object)
|
|
20
|
+
value = value.sub "{#{mask}}", mask_value.to_s unless mask_value.nil?
|
|
20
21
|
end
|
|
21
22
|
end
|
|
22
23
|
value
|
data/test/mask_test.rb
CHANGED
|
@@ -38,52 +38,59 @@ class MaskTest < ActiveSupport::TestCase
|
|
|
38
38
|
assert_equal 'Success', mask.unmask('SUCCESS', { :formatted => :capitalize })
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
test "should turn
|
|
42
|
-
mask = MaskableAttribute::Mask.new
|
|
41
|
+
test "should turn spaces into underscores for its name" do
|
|
42
|
+
mask = MaskableAttribute::Mask.new :"foo bar" => :foo_bar
|
|
43
43
|
|
|
44
|
-
assert_equal "
|
|
44
|
+
assert_equal "foo_bar", mask.name
|
|
45
45
|
assert_equal :foo_bar, mask.method
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
test "should return all accessed_by names" do
|
|
49
49
|
mask = MaskableAttribute::Mask.new :downcase => { :formats => { :capitalized => :capitalize } }
|
|
50
50
|
|
|
51
|
-
assert_equal ["
|
|
51
|
+
assert_equal ["capitalized_downcase", "downcase"], mask.accessed_by
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
test "should return all formatted exclusively accessed_by names" do
|
|
55
55
|
mask = MaskableAttribute::Mask.new :downcase => { :exclusive_format => { :capitalized => :capitalize } }
|
|
56
56
|
|
|
57
|
-
assert_equal ["
|
|
57
|
+
assert_equal ["capitalized_downcase"], mask.accessed_by
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
test "should determine whether a string matches its accessible names" do
|
|
61
61
|
mask = MaskableAttribute::Mask.new :downcase => { :exclusive_format => { :capitalized => :capitalize } }
|
|
62
62
|
|
|
63
63
|
assert !mask.accessed_by?("downcase")
|
|
64
|
-
assert mask.accessed_by?("
|
|
64
|
+
assert mask.accessed_by?("capitalized_downcase")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test "should accept two word formats with two word masks" do
|
|
68
|
+
mask = MaskableAttribute::Mask.new :down_cased => [ :downcase, :formats => { :capitalized_but => :capitalize } ]
|
|
69
|
+
|
|
70
|
+
assert mask.accessed_by?("capitalized_but_down_cased")
|
|
71
|
+
assert_equal 'Success', mask.unmask('SUCCESS', :formatted => :capitalized_but)
|
|
65
72
|
end
|
|
66
73
|
|
|
67
74
|
#describe masks
|
|
68
75
|
test "should find correct mask by accessor" do
|
|
69
|
-
masks = MaskableAttribute::Masks.new [ :bar => { :exclusive_format => { :capitalized => :capitalize } },
|
|
70
|
-
:baz => { :format => :upcase } ]
|
|
76
|
+
masks = MaskableAttribute::Masks.new [ { :bar => { :exclusive_format => { :capitalized => :capitalize } } },
|
|
77
|
+
{ :baz => { :format => :upcase } } ]
|
|
71
78
|
|
|
72
79
|
assert_equal "bar", masks.find_by_accessor("capitalized bar").name
|
|
73
80
|
end
|
|
74
81
|
|
|
75
82
|
test "should be accessible by accessor" do
|
|
76
|
-
masks = MaskableAttribute::Masks.new [ :bar => { :exclusive_format =>
|
|
77
|
-
:baz => { :format => :upcase } ]
|
|
83
|
+
masks = MaskableAttribute::Masks.new [ { :bar => { :exclusive_format => :capitalize } },
|
|
84
|
+
{ :baz => { :format => :upcase } } ]
|
|
78
85
|
|
|
79
|
-
assert_equal "
|
|
86
|
+
assert_equal "baz", masks["upcase baz"].name
|
|
80
87
|
end
|
|
81
88
|
|
|
82
|
-
test "should unmask given object with correct mask and format" do
|
|
83
|
-
masks = MaskableAttribute::Masks.new [ :downcase => { :exclusive_format =>
|
|
84
|
-
:bar => { :format => :upcase } ]
|
|
89
|
+
test "should unmask given object with correct mask and its exlusive format" do
|
|
90
|
+
masks = MaskableAttribute::Masks.new [ { :downcase => { :exclusive_format => :capitalize } },
|
|
91
|
+
{ :bar => { :format => :upcase } } ]
|
|
85
92
|
|
|
86
|
-
assert_equal "Success", masks["
|
|
93
|
+
assert_equal "Success", masks["capitalize downcase"].unmask("SUCCESS")
|
|
87
94
|
end
|
|
88
95
|
|
|
89
96
|
test "should return nil when trying to access and get value of a non-existent mask" do
|
|
@@ -116,9 +116,9 @@ class MaskableAttributeTest < ActiveSupport::TestCase
|
|
|
116
116
|
"ack one"
|
|
117
117
|
end
|
|
118
118
|
end
|
|
119
|
-
@
|
|
119
|
+
@fickwell = Fickwell.create! :bar => "{foo bar baz}"
|
|
120
120
|
|
|
121
|
-
assert_equal "ack one", @
|
|
121
|
+
assert_equal "ack one", @fickwell.bar, "Did not retrieve mask for object method reference"
|
|
122
122
|
end
|
|
123
123
|
|
|
124
124
|
test "masks should be able to directly reference aliased methods for the class object" do
|
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.2
|
|
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-06-
|
|
12
|
+
date: 2012-06-29 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rails
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &70349364129120 !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>='
|
|
@@ -21,7 +21,7 @@ dependencies:
|
|
|
21
21
|
version: 2.3.10
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements: *
|
|
24
|
+
version_requirements: *70349364129120
|
|
25
25
|
description: A maskable attribute is an attribute that is made up of other attributes
|
|
26
26
|
(masks), this ordering is set in the masked attribute and preserved across updates.
|
|
27
27
|
email:
|