Better encapsulation for Ruby instance variables
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
LICENSE
README.rdoc
Rakefile
ivar_encapsulation.gemspec

README.rdoc

ivar_encapsulation

Motivation

For some reason the direct manipulation of instance variables in Ruby has bothered me for a long time. But not until I read more about Smalltalk did I quite appreciate why that was. Compared to directly reading from or writing to the instance variables, using getters and setters also inside the class feels a lot cleaner, at least to me. Alas one doesn't always want to expose the methods generated by attr_* to the outside world, so I found myself repeatedly writing code similar to this:

attr_accessor :foo
private :foo=

While it certainly gets the job done, it's a bit cumbersome in the long run, so I decided to write this gem as an alternative to the available attr_* methods.

Requirements

A conflicted relationship with instance variables and Ruby 2.0+.

Installation

ivar_encapsulation can be installed via RubyGems.

$ gem install ivar_encapsulation

It only works on 2.0+ and I generally only test it with the most current Ruby release.

Usage

The most simple case is defining a purely internal attribute (both the getter and setter will be private):

class Foo
  attribute :foo
end

Of course this also works for several attributes at the same time:

attribute :foo, :bar, :baz

For the grammatically anal like yours truly, attribute is aliased to attributes, so the above could also be written thus:

attributes :foo, :bar, :baz

Now for a more interesting use case:

attribute foo, getter: true

This will define an attribute with a public getter method (foo), while the setter (foo=) remains private. Essentially this is the same as doing the following:

attr_accessor :foo
private :foo=

Once again this works for several attributes at the same time:

attribute :foo, :bar, :baz, getter: true

For completeness' sake, you can also define a private getter and a public setter, although I don't think I ever needed this specific case:

attribute :foo, setter: true

Last but not least there's the option to have both the getter and the setter as public methods, which is essentially the same as attr_accessor, although in a more explicit fashion. If you bother to use this gem at all, you probably want to define all your attributes in a consistent fashion:

attribute :foo, getter: true, setter: true

This is how your class definitions will look when using this gem:

class Foo
  attributes :foo, :bar                              # private getters and setters
  attribute :read_only, getter: true                 # public getter, private setter
  attribute :full_access, getter: true, setter: true # public getter and setter

  def initialize(foo)
    self.foo = foo
    ...
  end

  ...

end

Todo/Ideas

  • Being able to supply getters and setters as procs?

Authors

This project was created by Michael Kohl. He can be reached via email at citizen428 at gmail.com.

License

Copyright © 2013 Michael Kohl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.