Skip to content

jswanner/private_attr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PrivateAttr Build Status

Perhaps it's just personal preference, but I don't like to use instance variables througout my Ruby classes. At the same time, I don't necessarily want public attribute readers and writers. This causes me to tend to use something like the following:

class Foo
  def initialize bar
    self.bar = bar
  end

  private

  attr_accessor :bar
end

Unfortunately, this makes Ruby show a ‘private attribute?’ warning. Also, I really like to declare my attribute readers and writers near the top of my classes, which means I sometimes use something like:

class Bar
  attr_accessor :foo
  private :foo, :foo=

  def initialize foo
    self.foo = foo
  end
end

This library is meant to be a convenient solution to that verbosity.

Installation

Add this line to your application's Gemfile:

gem 'private_attr'

And then execute:

$ bundle

Or install it yourself as:

$ gem install private_attr

Usage

class Bar
  extend PrivateAttr

  private_attr_accessor :foo
  private_attr_reader :baz
  private_attr_writer :bat
  private_alias_method :bar, :foo

  def initialize foo
    self.foo = foo
  end
end

It even does protected attributes:

class Bar
  extend PrivateAttr

  protected_attr_accessor :foo
  protected_attr_reader :baz
  protected_attr_writer :bat

  def initialize foo
    self.foo = foo
  end
end

If you don’t want to extend all of your modules and classes separately you can also require 'private_attr/everywhere' and the methods will be available everywhere.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Easily create private/protected attribute readers/writers

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages