Sha256: 63143435e7f9a4e97633dbc81004037e22c0f5ab9ffd753aa41d823ecd7813b5

Contents?: true

Size: 1011 Bytes

Versions: 23

Compression:

Stored size: 1011 Bytes

Contents

# From:
# http://www.bigbold.com/snippets/posts/show/2178
# http://blog.caboo.se/articles/2006/06/11/stupid-hash-tricks
# 
# An example utilisation of these methods in a controller is:
# def some_action
#    # some script kiddie also passed in :bee, which we don't want tampered with _here_.
#    @model = Model.create(params.pass(:foo, :bar))
#  end
class Hash

  # lets through the keys in the argument
  # >> {:one => 1, :two => 2, :three => 3}.pass(:one)
  # => {:one=>1}
  def pass(*keys)
    keys = keys.first if keys.first.is_a?(Array)
    tmp = self.clone
    tmp.delete_if {|k,v| ! keys.include?(k.to_sym) }
    tmp.delete_if {|k,v| ! keys.include?(k.to_s) }
    tmp
  end

  # blocks the keys in the arguments
  # >> {:one => 1, :two => 2, :three => 3}.block(:one)
  # => {:two=>2, :three=>3}
  def block(*keys)
    keys = keys.first if keys.first.is_a?(Array)
    tmp = self.clone
    tmp.delete_if {|k,v| keys.include?(k.to_sym) }
    tmp.delete_if {|k,v| keys.include?(k.to_s) }
    tmp
  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
composite_primary_keys-3.1.11 test/hash_tricks.rb
composite_primary_keys-4.1.2 test/hash_tricks.rb
composite_primary_keys-4.1.1 test/hash_tricks.rb
composite_primary_keys-4.0.0 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta9 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta8 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta7 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta6 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta5 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta4 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta3 test/hash_tricks.rb
composite_primary_keys-3.1.10 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta2 test/hash_tricks.rb
composite_primary_keys-4.0.0.beta1 test/hash_tricks.rb
composite_primary_keys-3.1.9 test/hash_tricks.rb
composite_primary_keys-3.1.8 test/hash_tricks.rb
composite_primary_keys-3.1.7 test/hash_tricks.rb
composite_primary_keys-3.1.6 test/hash_tricks.rb
composite_primary_keys-3.1.5 test/hash_tricks.rb
composite_primary_keys-3.1.4 test/hash_tricks.rb