whitelist 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ben Alavi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ Whitelist
2
+ =======
3
+
4
+ Easy to use filter for parameter hashes.
5
+
6
+ Description
7
+ -----------
8
+
9
+ Whitelist helps you filter a parameters hash to get only the values you
10
+ care about.
11
+
12
+ It's a very common pattern to forbid access to key attributes in models.
13
+ One obvious example is ActiveRecord's use of the attr_accessible
14
+ decorator.
15
+
16
+ The problem with ActiveRecord's approach is that it assumes you want the
17
+ same restrictions to apply for every context, which is not always the
18
+ case. A common scenario is to have different access levels for different
19
+ kinds of users, or higher restrictions for user generated data and a
20
+ lower barrier for developer generated data.
21
+
22
+ Whitelist approaches this problem by providing a simple mechanism for
23
+ filtering hashes, with some fair assumptions to abstract recurring
24
+ patterns.
25
+
26
+ Usage
27
+ -----
28
+
29
+ params[:foo] = nil
30
+
31
+ # Returns an empty hash if the value provided is nil.
32
+ whitelist(params[:foo]) #=> {}
33
+
34
+ params[:foo] = { :bar => 1, :baz => 2 }
35
+
36
+ # Returns a copy of params[:hash] with the keys provided.
37
+ whitelist(params[:foo], :bar) #=> { :bar => 1 }
38
+
39
+ # It also accepts a symbol as the first parameter, and interprets it as a key for params.
40
+ whitelist(:foo, :bar) #=> { :bar => 1 }
41
+ whitelist(:foo) #=> {}
42
+
43
+ Installation
44
+ ------------
45
+
46
+ $ sudo gem install whitelist
47
+
48
+ License
49
+ -------
50
+
51
+ Copyright (c) 2009 Ben Alavi
52
+
53
+ Permission is hereby granted, free of charge, to any person
54
+ obtaining a copy of this software and associated documentation
55
+ files (the "Software"), to deal in the Software without
56
+ restriction, including without limitation the rights to use,
57
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
58
+ copies of the Software, and to permit persons to whom the
59
+ Software is furnished to do so, subject to the following
60
+ conditions:
61
+
62
+ The above copyright notice and this permission notice shall be
63
+ included in all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
66
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
67
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
68
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
69
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
70
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
71
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
72
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.pattern = 'test/**/*_test.rb'
7
+ t.verbose = false
8
+ end
@@ -0,0 +1,35 @@
1
+ module Whitelist
2
+
3
+ # Whitelist a hash by filtering the provided keys.
4
+ #
5
+ # @overload whitelist hash, :foo, :bar, :baz
6
+ # Returns a copy of hash with the keys provided.
7
+ # @overload whitelist :hash, :foo, :bar, :baz
8
+ # Returns a copy of params[:hash] with the keys provided.
9
+ # @example Different ways of using whitelist:
10
+ # params[:foo] = nil
11
+
12
+ # # Returns an empty hash if the value provided is nil.
13
+ # whitelist(params[:foo]) #=> {}
14
+
15
+ # params[:foo] = { :bar => 1, :baz => 2 }
16
+
17
+ # # Returns a copy of params[:hash] with the keys provided.
18
+ # whitelist(params[:foo], :bar) #=> { :bar => 1 }
19
+
20
+ # # It also accepts a symbol as the first parameter, and interprets it as a key for params.
21
+ # whitelist(:foo, :bar) #=> { :bar => 1 }
22
+ # whitelist(:foo) #=> {}
23
+ def whitelist(hash, *keys)
24
+ return {} if hash.nil?
25
+ return {} if keys.empty?
26
+ return whitelist(params[hash], *keys) if hash.is_a?(Symbol)
27
+
28
+ result = Hash.new
29
+ keys.each do |key|
30
+ next unless hash.member?(key)
31
+ result[key] = hash[key]
32
+ end
33
+ result
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'contest'
3
+ require File.dirname(__FILE__) + "/../lib/whitelist"
4
+
5
+ def params
6
+ { :a => 1, :b => 2, :c => { :d => 3 } }
7
+ end
8
+
9
+ include Whitelist
10
+
11
+ class TestWhitelist < Test::Unit::TestCase
12
+ setup do
13
+ @hash = { :a => 1, :b => 2, :c => { :d => 3 } }
14
+ end
15
+
16
+ context "without a whitelist " do
17
+ should "return an empty hash" do
18
+ assert_equal Hash.new, whitelist(@hash)
19
+ end
20
+ end
21
+
22
+ context "with a nil value as the first parameter" do
23
+ should "also return an empty hash" do
24
+ assert_equal Hash.new, whitelist(nil)
25
+ end
26
+ end
27
+
28
+ context "with a symbol as the first parameter" do
29
+ should "pull the hash from the params method" do
30
+ assert_equal @hash[:c], whitelist(:c, :d)
31
+ end
32
+
33
+ should "return an empty hash if the key is not found" do
34
+ assert_equal Hash.new, whitelist(:c, :f)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'whitelist'
3
+ s.version = '0.0.1'
4
+ s.summary = %{Easy to use filter for parameter hashes}
5
+ s.description = %{Whitelist helps you filter a parameters hash to get only the values you care about.}
6
+ s.authors = ["Ben Alavi"]
7
+ s.email = ["ben.alavi@citrusbyte.com"]
8
+ s.homepage = "http://github.com/citrusbyte/whitelist"
9
+ s.files = ["lib/whitelist.rb", "README.markdown", "LICENSE", "Rakefile", "test/all_test.rb", "whitelist.gemspec"]
10
+ s.rubyforge_project = "whitelist"
11
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whitelist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Alavi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-30 00:00:00 -03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Whitelist helps you filter a parameters hash to get only the values you care about.
17
+ email:
18
+ - ben.alavi@citrusbyte.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/whitelist.rb
27
+ - README.markdown
28
+ - LICENSE
29
+ - Rakefile
30
+ - test/all_test.rb
31
+ - whitelist.gemspec
32
+ has_rdoc: true
33
+ homepage: http://github.com/citrusbyte/whitelist
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project: whitelist
56
+ rubygems_version: 1.3.4
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Easy to use filter for parameter hashes
60
+ test_files: []
61
+