Created
January 4, 2010 20:10
-
-
Save ognevsky/268818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sneaky attr_accessible clone for MongoMapper | |
class Filter | |
def initialize(hash) | |
@hash = hash | |
end | |
def accessible(*values) | |
@accessible = [] | |
values.each {|value| @accessible<< value } | |
end | |
def filter | |
@hash.each_pair{|key, value| @accessible.include?(key.to_sym) ? next : @hash.delete(key)} | |
end | |
end | |
# Some test hash | |
some_param = {:title => "Some title", :content => "Some content", :author => "Some author" } | |
a = Filter.new(some_param) | |
# This line is similar to attr_accessible. Need to type here array of keys | |
a.accessible :title | |
# This will puts our filtered "some_param" hash | |
a.filter.each_pair {|key, value| puts "#{key}:#{value}"} # => title:Some title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment