Skip to content

Instantly share code, notes, and snippets.

@ognevsky
Created January 4, 2010 20:10
Show Gist options
  • Save ognevsky/268818 to your computer and use it in GitHub Desktop.
Save ognevsky/268818 to your computer and use it in GitHub Desktop.
# 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