maskable_attribute 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.
Files changed (67) hide show
  1. data/.gitignore +7 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +17 -0
  4. data/Gemfile.lock +37 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +56 -0
  7. data/Rakefile +37 -0
  8. data/lib/maskable_attribute.rb +6 -0
  9. data/lib/maskable_attribute/acts_as_maskable_attribute.rb +48 -0
  10. data/lib/maskable_attribute/formatting.rb +71 -0
  11. data/lib/maskable_attribute/mask.rb +112 -0
  12. data/lib/maskable_attribute/maskable_attribute.rb +58 -0
  13. data/lib/maskable_attribute/version.rb +3 -0
  14. data/lib/tasks/maskable_attribute_tasks.rake +4 -0
  15. data/maskable_attribute.gemspec +22 -0
  16. data/test/dummy/Rakefile +10 -0
  17. data/test/dummy/app/controllers/application_controller.rb +10 -0
  18. data/test/dummy/app/helpers/application_helper.rb +3 -0
  19. data/test/dummy/app/models/hickwell.rb +6 -0
  20. data/test/dummy/config/boot.rb +128 -0
  21. data/test/dummy/config/database.yml +22 -0
  22. data/test/dummy/config/environment.rb +41 -0
  23. data/test/dummy/config/environments/development.rb +17 -0
  24. data/test/dummy/config/environments/production.rb +28 -0
  25. data/test/dummy/config/environments/test.rb +28 -0
  26. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  27. data/test/dummy/config/initializers/cookie_verification_secret.rb +7 -0
  28. data/test/dummy/config/initializers/inflections.rb +10 -0
  29. data/test/dummy/config/initializers/mime_types.rb +5 -0
  30. data/test/dummy/config/initializers/new_rails_defaults.rb +21 -0
  31. data/test/dummy/config/initializers/session_store.rb +15 -0
  32. data/test/dummy/config/locales/en.yml +5 -0
  33. data/test/dummy/config/preinitializer.rb +20 -0
  34. data/test/dummy/config/routes.rb +43 -0
  35. data/test/dummy/db/migrate/20111220153853_create_hickwells.rb +15 -0
  36. data/test/dummy/db/schema.rb +23 -0
  37. data/test/dummy/db/seeds.rb +7 -0
  38. data/test/dummy/public/404.html +30 -0
  39. data/test/dummy/public/422.html +30 -0
  40. data/test/dummy/public/500.html +30 -0
  41. data/test/dummy/public/favicon.ico +0 -0
  42. data/test/dummy/public/images/rails.png +0 -0
  43. data/test/dummy/public/index.html +275 -0
  44. data/test/dummy/public/javascripts/application.js +2 -0
  45. data/test/dummy/public/javascripts/controls.js +963 -0
  46. data/test/dummy/public/javascripts/dragdrop.js +973 -0
  47. data/test/dummy/public/javascripts/effects.js +1128 -0
  48. data/test/dummy/public/javascripts/prototype.js +4320 -0
  49. data/test/dummy/public/robots.txt +5 -0
  50. data/test/dummy/script/about +4 -0
  51. data/test/dummy/script/console +3 -0
  52. data/test/dummy/script/dbconsole +3 -0
  53. data/test/dummy/script/destroy +3 -0
  54. data/test/dummy/script/generate +3 -0
  55. data/test/dummy/script/performance/benchmarker +3 -0
  56. data/test/dummy/script/performance/profiler +3 -0
  57. data/test/dummy/script/plugin +3 -0
  58. data/test/dummy/script/runner +3 -0
  59. data/test/dummy/script/server +3 -0
  60. data/test/dummy/test/fixtures/hickwells.yml +9 -0
  61. data/test/dummy/test/test_helper.rb +40 -0
  62. data/test/dummy/test/unit/hickwell_test.rb +31 -0
  63. data/test/formatting_test.rb +71 -0
  64. data/test/mask_test.rb +95 -0
  65. data/test/maskable_attribute_test.rb +163 -0
  66. data/test/test_helper.rb +10 -0
  67. metadata +174 -0
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ $LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info"
4
+ require 'commands/about'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ require 'commands/console'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ require 'commands/dbconsole'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ require 'commands/destroy'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ require 'commands/generate'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../../config/boot', __FILE__)
3
+ require 'commands/performance/benchmarker'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../../config/boot', __FILE__)
3
+ require 'commands/performance/profiler'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ require 'commands/plugin'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ require 'commands/runner'
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../config/boot', __FILE__)
3
+ require 'commands/server'
@@ -0,0 +1,9 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ one:
4
+ foo: MyString
5
+ bar: MyString
6
+
7
+ two:
8
+ foo: MyString
9
+ bar: MyString
@@ -0,0 +1,40 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
5
+ require 'test_help'
6
+
7
+ class ActiveSupport::TestCase
8
+ # Transactional fixtures accelerate your tests by wrapping each test method
9
+ # in a transaction that's rolled back on completion. This ensures that the
10
+ # test database remains unchanged so your fixtures don't have to be reloaded
11
+ # between every test method. Fewer database queries means faster tests.
12
+ #
13
+ # Read Mike Clark's excellent walkthrough at
14
+ # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
15
+ #
16
+ # Every Active Record database supports transactions except MyISAM tables
17
+ # in MySQL. Turn off transactional fixtures in this case; however, if you
18
+ # don't care one way or the other, switching from MyISAM to InnoDB tables
19
+ # is recommended.
20
+ #
21
+ # The only drawback to using transactional fixtures is when you actually
22
+ # need to test transactions. Since your test is bracketed by a transaction,
23
+ # any transactions started in your code will be automatically rolled back.
24
+ self.use_transactional_fixtures = true
25
+
26
+ # Instantiated fixtures are slow, but give you @david where otherwise you
27
+ # would need people(:david). If you don't want to migrate your existing
28
+ # test cases which use the @david style and don't mind the speed hit (each
29
+ # instantiated fixtures translates to a database query per test method),
30
+ # then set this back to true.
31
+ self.use_instantiated_fixtures = false
32
+
33
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
34
+ #
35
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
36
+ # -- they do not yet inherit this setting
37
+ fixtures :all
38
+
39
+ # Add more helper methods to be used by all tests here...
40
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class HickwellTest < ActiveSupport::TestCase
4
+ def setup
5
+ @hickwell = Hickwell.create!
6
+ end
7
+
8
+ test "should have 4 attributes, foo, bar, baz, qux" do
9
+ assert Hickwell.column_names.include?("foo"), "Missing attribute: foo"
10
+ assert Hickwell.column_names.include?("bar"), "Missing attribute: bar"
11
+ assert Hickwell.column_names.include?("baz"), "Missing attribute: baz"
12
+ assert Hickwell.column_names.include?("qux"), "Missing attribute: qux"
13
+ end
14
+
15
+ test "should have maskable_attribute qux" do
16
+ assert @hickwell.maskable_qux.is_a?(MaskableAttribute::MaskableAttribute), "Masked attribute isn't a MaskableAttribute"
17
+ end
18
+
19
+ test "should have getter and setter methods for qux" do
20
+ assert_respond_to @hickwell, :qux, "No getter method"
21
+ assert_respond_to @hickwell, :qux=, "No setter method"
22
+ end
23
+
24
+ test "should have a quux method" do
25
+ assert @hickwell.respond_to? :quux, "Missing quux method"
26
+ end
27
+
28
+ test "returns 'thud' from quux method" do
29
+ assert_equal 'thud', @hickwell.quux, "#quux not returning 'thud'"
30
+ end
31
+ end
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class FormattingTest < ActiveSupport::TestCase
4
+
5
+ #describe Format
6
+ test "should accept a symbol as the format method" do
7
+ format = MaskableAttribute::Formatting::Format.new :upcase
8
+ assert_equal "SUCCESS", format.apply("success")
9
+ end
10
+
11
+ test "should accept a Proc as the format method" do
12
+ format = MaskableAttribute::Formatting::Format.new Proc.new { |value| value.upcase }
13
+ assert_equal "SUCCESS", format.apply("success")
14
+ end
15
+
16
+ #describe Formats
17
+ test "should accept a hash of a single format" do
18
+ formats = MaskableAttribute::Formatting::Formats.new :formats => :upcase
19
+
20
+ assert_equal "SUCCESS", formats[:upcase].apply("success")
21
+ end
22
+
23
+ test "should accept a hash of an array of formats" do
24
+ formats = MaskableAttribute::Formatting::Formats.new :formats => [ :upcase, :downcase ]
25
+
26
+ assert_equal "SUCCESS", formats[:upcase].apply("success"), "First element not assigned as name/method"
27
+ assert_equal "success", formats[:downcase].apply("SUCCESS"), "Second element not assigned as name/method"
28
+ end
29
+
30
+ test "should accept a hash of a hash with a symbol as a value" do
31
+ formats = MaskableAttribute::Formatting::Formats.new :formats => { :upcased => :upcase }
32
+
33
+ assert_equal "SUCCESS", formats[:upcased].apply("success")
34
+ end
35
+
36
+ test "should accept a hash of a hash of formats" do
37
+ formats = MaskableAttribute::Formatting::Formats.new :formats => { :two_digit => Proc.new { |value| format '%02d', value },
38
+ :upcased => :upcase,
39
+ :downcase => nil }
40
+
41
+ assert_equal '01', formats[:two_digit].apply(1), "Proc not used as method"
42
+ assert_equal "SUCCESS", formats[:upcased].apply("success"), "Symbol not used as method"
43
+ assert_equal "success", formats[:downcase].apply("SUCCESS"), "Name, in leu of nil, not used as method"
44
+ end
45
+
46
+ test "should recognize exclusive formats" do
47
+ formats = MaskableAttribute::Formatting::Formats.new :exclusive_formats => [ :upcase, :downcase ]
48
+
49
+ assert formats.are_exclusive?, "Did not recognize exclusive formats"
50
+ end
51
+
52
+ test "should recognize a default_format" do
53
+ formats = MaskableAttribute::Formatting::Formats.new :default_format => :upcase
54
+
55
+ assert formats.has_default?
56
+ assert_equal formats[:upcase], formats.default, "Default format not assigned"
57
+ assert_equal 'SUCCESS', formats.default.apply("success"), "Default format not applied"
58
+ end
59
+
60
+ test "should be able to use a block for applying formats" do
61
+ formats = MaskableAttribute::Formatting::Formats.new :formats => :upcase
62
+
63
+ assert_equal 'SUCCESS', formats.apply(:upcase) { "success" }
64
+ end
65
+
66
+ test "should provide all format names" do
67
+ formats = MaskableAttribute::Formatting::Formats.new :formats => [ :upcase, :downcase ]
68
+
69
+ assert_equal [ :upcase, :downcase ], formats.names, "Did not provide all format names"
70
+ end
71
+ end
data/test/mask_test.rb ADDED
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ class MaskTest < ActiveSupport::TestCase
4
+
5
+ #describe mask
6
+ test "should accept symbol as both a name and its method" do
7
+ mask = MaskableAttribute::Mask.new :upcase
8
+
9
+ assert_equal "upcase", mask.name
10
+ assert_equal 'SUCCESS', mask.unmask('success')
11
+ end
12
+
13
+ test "should accept symbol as a name and method in its hash" do
14
+ mask = MaskableAttribute::Mask.new :lowercase => :downcase
15
+
16
+ assert_equal 'lowercase', mask.name
17
+ assert_equal 'success', mask.unmask('SUCCESS')
18
+ end
19
+
20
+ test "should accept proc as a method in its hash" do
21
+ mask = MaskableAttribute::Mask.new :lowercase => Proc.new { |value| value.downcase }
22
+
23
+ assert_equal 'lowercase', mask.name
24
+ assert_equal 'success', mask.unmask('SUCCESS')
25
+ end
26
+
27
+ test "should accept a hash of hash for options" do
28
+ mask = MaskableAttribute::Mask.new :downcase => { :formats => { :capitalized => :capitalize } }
29
+
30
+ assert_equal 'downcase', mask.name
31
+ assert_equal 'Success', mask.unmask('SUCCESS', :formatted => :capitalized)
32
+ end
33
+
34
+ test "should accept a hash of an array for options" do
35
+ mask = MaskableAttribute::Mask.new :lowercase => [ :downcase, { :formats => :capitalize } ]
36
+
37
+ assert_equal 'lowercase', mask.name
38
+ assert_equal 'Success', mask.unmask('SUCCESS', { :formatted => :capitalize })
39
+ end
40
+
41
+ test "should turn underscores into spaces for its name" do
42
+ mask = MaskableAttribute::Mask.new :foo_bar
43
+
44
+ assert_equal "foo bar", mask.name
45
+ assert_equal :foo_bar, mask.method
46
+ end
47
+
48
+ test "should return all accessed_by names" do
49
+ mask = MaskableAttribute::Mask.new :downcase => { :formats => { :capitalized => :capitalize } }
50
+
51
+ assert_equal ["capitalized downcase", "downcase"], mask.accessed_by
52
+ end
53
+
54
+ test "should return all formatted exclusively accessed_by names" do
55
+ mask = MaskableAttribute::Mask.new :downcase => { :exclusive_format => { :capitalized => :capitalize } }
56
+
57
+ assert_equal ["capitalized downcase"], mask.accessed_by
58
+ end
59
+
60
+ test "should determine whether a string matches its accessible names" do
61
+ mask = MaskableAttribute::Mask.new :downcase => { :exclusive_format => { :capitalized => :capitalize } }
62
+
63
+ assert !mask.accessed_by?("downcase")
64
+ assert mask.accessed_by?("capitalized downcase")
65
+ end
66
+
67
+ #describe masks
68
+ test "should find correct mask by accessor" do
69
+ masks = MaskableAttribute::Masks.new [ :bar => { :exclusive_format => { :capitalized => :capitalize } },
70
+ :baz => { :format => :upcase } ]
71
+
72
+ assert_equal "bar", masks.find_by_accessor("capitalized bar").name
73
+ end
74
+
75
+ test "should be accessible by accessor" do
76
+ masks = MaskableAttribute::Masks.new [ :bar => { :exclusive_format => { :capitalized => :capitalize } },
77
+ :baz => { :format => :upcase } ]
78
+
79
+ assert_equal "bar", masks["capitalized bar"].name
80
+ end
81
+
82
+ test "should unmask given object with correct mask and format" do
83
+ masks = MaskableAttribute::Masks.new [ :downcase => { :exclusive_format => { :capitalized => :capitalize } },
84
+ :bar => { :format => :upcase } ]
85
+
86
+ assert_equal "Success", masks["capitalized downcase"].unmask("SUCCESS")
87
+ end
88
+
89
+ test "should return nil when trying to access and get value of a non-existent mask" do
90
+ masks = MaskableAttribute::Masks.new [ :downcase => { :exclusive_format => { :capitalized => :capitalize } },
91
+ :bar => { :format => :upcase } ]
92
+
93
+ assert_nil masks["not a real mask"].unmask("nil is success!"), "This will probably be an error..."
94
+ end
95
+ end
@@ -0,0 +1,163 @@
1
+ require 'test_helper'
2
+
3
+ class MaskableAttributeTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, MaskableAttribute
6
+ end
7
+
8
+ test "should be able to set an attribute to be maskable" do
9
+ assert_respond_to Hickwell, :maskable_attribute, "Can't mask an attribute"
10
+ end
11
+
12
+ test "should be able to get available masks" do
13
+ @hickwell = Hickwell.create!
14
+
15
+ assert_respond_to @hickwell.maskable_qux, :masks, "Couldn't get available masks"
16
+ end
17
+
18
+ test "should get available masks" do
19
+ @hickwell = Hickwell.create!
20
+
21
+ assert_equal [ :foo, :bar, :baz ], @hickwell.maskable_qux.masks, "Masks method did not return list of masks"
22
+ end
23
+
24
+ test "should have masked_object available to itself" do
25
+ @hickwell = Hickwell.create!
26
+
27
+ assert_equal @hickwell, @hickwell.maskable_qux.masked_object, "Couldn't determine the masked object"
28
+ end
29
+
30
+ test "should be able to set masking of attribute" do
31
+ @hickwell = Hickwell.create!
32
+
33
+ @hickwell.qux = "{foo}{bar}{baz}"
34
+ assert_equal "{foo}{bar}{baz}", @hickwell.read_attribute(:qux), "Couldn't set masking of attribute"
35
+ end
36
+
37
+ test "should not overwrite attribute with unmasked attribute" do
38
+ @hickwell = Hickwell.create! :foo => "a", :bar => "b", :baz => "c", :qux => "{foo}{bar}{baz}"
39
+
40
+ assert_equal "abc", @hickwell.qux
41
+ assert_equal "abc", @hickwell.qux
42
+ assert_equal "{foo}{bar}{baz}", @hickwell.read_attribute(:qux), "Overwriting attribute with unmasked value"
43
+ end
44
+
45
+ test "should be able to get attribute masked (by default)" do
46
+ @hickwell = Hickwell.create! :foo => "a", :bar => "b", :baz => "c", :qux => "{foo}{bar}{baz}"
47
+
48
+ assert_equal "abc", @hickwell.qux, "Couldn't get attribute masked"
49
+ end
50
+
51
+ test "should be able to get attribute unmasked" do
52
+ @hickwell = Hickwell.create! :foo => "a", :bar => "b", :baz => "c", :qux => "{foo}{bar}{baz}"
53
+
54
+ assert_equal "{foo}{bar}{baz}", @hickwell.maskable_qux.unmasked, "Could not get attribute unmasked"
55
+ end
56
+
57
+ test "should be able to get set value of attribute and have masks persist" do
58
+ @hickwell = Hickwell.create! :foo => "a", :bar => "b", :baz => "c", :qux => "{foo}{bar}{baz}"
59
+ @hickwell.qux = "bac"
60
+
61
+ assert_equal "{bar}{foo}{baz}", @hickwell.maskable_qux.unmasked, "Masks didn't persist though update"
62
+ end
63
+
64
+ test "masks should be able to reference differently named methods" do
65
+ class Rickwell < Hickwell
66
+ maskable_attribute :bar, :qux => :quux
67
+ end
68
+
69
+ @hickwell = Rickwell.create! :bar => "{qux}"
70
+
71
+ assert_equal "thud", @hickwell.bar
72
+ end
73
+
74
+ test "should allow maskable_attribute to be nil" do
75
+ @hickwell = Hickwell.create! :foo => "a", :bar => "b", :baz => "c", :qux => "{foo}{bar}{baz}"
76
+ @hickwell.qux = nil
77
+
78
+ assert_nil @hickwell.qux, "Maskable attribute not set to nil"
79
+ end
80
+
81
+ test "masks should be able to reference a Proc block" do
82
+ class Wickwell < Hickwell
83
+ maskable_attribute :baz, :ack => Proc.new { "syn" }
84
+ end
85
+
86
+ @hickwell = Wickwell.create! :baz => "{ack}"
87
+
88
+ assert_equal "syn", @hickwell.baz
89
+ end
90
+
91
+ test "masks should be able to handle multiple words" do
92
+ class Dickwell < Hickwell
93
+ maskable_attribute :bar, :foo_bar => Proc.new { "syn" }
94
+ end
95
+
96
+ @hickwell = Dickwell.create! :bar => "{foo_bar}"
97
+
98
+ assert_equal "syn", @hickwell.bar, "Did not retrieve mask for multiple words"
99
+ end
100
+
101
+ test "masks should be able to handle multiple words with spaces" do
102
+ class Zickwell < Hickwell
103
+ maskable_attribute :bar, :foo_bar => Proc.new { "syn" }
104
+ end
105
+
106
+ @hickwell = Zickwell.create! :bar => "{foo bar}"
107
+
108
+ assert_equal "syn", @hickwell.bar, "Did not retrieve mask for multiple words with spaces"
109
+ end
110
+
111
+ test "masks should be able to directly reference methods for the class object" do
112
+ class Fickwell < Hickwell
113
+ maskable_attribute :bar, [ :foo_bar_baz ]
114
+
115
+ def foo_bar_baz
116
+ "ack one"
117
+ end
118
+ end
119
+ @hickwell = Fickwell.create! :bar => "{foo bar baz}"
120
+
121
+ assert_equal "ack one", @hickwell.bar, "Did not retrieve mask for object method reference"
122
+ end
123
+
124
+ test "masks should be able to directly reference aliased methods for the class object" do
125
+ class Fickwell < Hickwell
126
+ maskable_attribute :bar, [ :fbb ]
127
+
128
+ def foo_bar_baz
129
+ "syn two"
130
+ end
131
+
132
+ alias :fbb :foo_bar_baz
133
+ end
134
+ @hickwell = Fickwell.create! :bar => "{fbb}"
135
+
136
+ assert_equal "syn two", @hickwell.bar, "Did not retrieve mask for object aliased method reference"
137
+ end
138
+
139
+ test "should be able to handle masks for non-string types" do
140
+ class Nickwell < Hickwell
141
+ maskable_attribute :baz, [ :number ]
142
+
143
+ def number
144
+ 5
145
+ end
146
+ end
147
+ @hickwell = Nickwell.create! :baz => "fixture {number}"
148
+
149
+ assert_equal "fixture 5", @hickwell.baz, "Could not handle non-string type mask"
150
+ end
151
+
152
+ test "should raise exception if maskable_attribute isn't actually an attribute" do
153
+ assert_raise ArgumentError do
154
+ Hickwell.maskable_attribute :fail, [ :foo, :bar, :baz ]
155
+ end
156
+ end
157
+
158
+ test "should raise exception if no masks are passed" do
159
+ assert_raise ArgumentError do
160
+ Hickwell.maskable_attribute :qux
161
+ end
162
+ end
163
+ end