ggoodale / attr_private

attr_private provides a way of making fields on an ActiveRecord model inaccessible to users of that model.

This URL has Read+Write access

ggoodale (author)
Sat Oct 18 13:47:42 -0700 2008
commit  764f476c10abaaa25c2ea4b01c9503e1099f1e7f
tree    02cf4b76cedac38c6b1b9ca4a7c6ddb8bbc1c245
parent  e785dd634c332cbb5307755a1b667038b811fbd7
name age message
file .gitignore Loading commit data...
file CHANGES
file README.rdoc
file Rakefile
file attr_private.gemspec
directory lib/
directory spec/
README.rdoc

attr_private

attr_private is a way to hide fields on an ActiveRecord model such that they are inaccessible to external callers. fields declared private are protected from most access methods provided by AR. Attempts to access a private field will raise a NoMethodError as though the field did not exist.

Using attr_private

app/models/my_model.rb

  class MyModel < ActiveRecord::Base
    attr_private :my_field, :some_other_field
  end

Limitations

Currently, attr_private doesn’t prevent reading via the attributes hash.

Special Thanks To

  • reQall for letting me hack in Ruby
  • Evan Light for pushing me to make this a gem (my first!)