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