]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/composite_primary_keys-1.1.0/test/hash_tricks.rb
The render method needs to be last, otherwise the error header won't get set. Needs...
[rails.git] / vendor / gems / composite_primary_keys-1.1.0 / test / hash_tricks.rb
1 # From:\r
2 # http://www.bigbold.com/snippets/posts/show/2178\r
3 # http://blog.caboo.se/articles/2006/06/11/stupid-hash-tricks\r
4\r
5 # An example utilisation of these methods in a controller is:\r
6 # def some_action\r
7 #    # some script kiddie also passed in :bee, which we don't want tampered with _here_.\r
8 #    @model = Model.create(params.pass(:foo, :bar))\r
9 #  end\r
10 class Hash\r
11 \r
12   # lets through the keys in the argument\r
13   # >> {:one => 1, :two => 2, :three => 3}.pass(:one)\r
14   # => {:one=>1}\r
15   def pass(*keys)\r
16     keys = keys.first if keys.first.is_a?(Array)\r
17     tmp = self.clone\r
18     tmp.delete_if {|k,v| ! keys.include?(k.to_sym) }\r
19     tmp.delete_if {|k,v| ! keys.include?(k.to_s) }\r
20     tmp\r
21   end\r
22 \r
23   # blocks the keys in the arguments\r
24   # >> {:one => 1, :two => 2, :three => 3}.block(:one)\r
25   # => {:two=>2, :three=>3}\r
26   def block(*keys)\r
27     keys = keys.first if keys.first.is_a?(Array)\r
28     tmp = self.clone\r
29     tmp.delete_if {|k,v| keys.include?(k.to_sym) }\r
30     tmp.delete_if {|k,v| keys.include?(k.to_s) }\r
31     tmp\r
32   end\r
33 \r
34 end\r