README.rdoc

Path: README.rdoc
Last Update: Sun Dec 14 21:59:48 -0500 2008

clafamatt

    by Avdi Grimm
    http://clafamatt.rubyforge.org

DESCRIPTION:

ClaFamAtt (CLAss FAMily ATTributes) is class inheritable attributes without the baggage.

FEATURES

  • Fully Spec‘d
  • reader/writer methods are cleanly partitioned in dynamically created modules
  • No extra class variables needed for bookkeeping
  • Family inheritable attributes can be defined in modules.
  • Only there when you want it - will not pollute the global namespace.

SYNOPSIS:

  module Shared
    include Clafamatt::Macros

    class_family_accessor :foo
  end

  class Parent
    include Shared

  end

  class Child < Parent
    class_family_accessor :bar
  end

  Shared.foo                      # => nil
  Parent.foo                      # => nil
  Child.foo                       # => nil

  Shared.foo = "klaatu"
  Shared.foo                      # => "klaatu"
  Parent.foo                      # => "klaatu"
  Child.foo                       # => "klaatu"

  Parent.foo = "nikto"
  Shared.foo                      # => "klaatu"
  Parent.foo                      # => "nikto"
  Child.foo                       # => "nikto"

  Child.foo = "barada"
  Shared.foo                      # => "klaatu"
  Parent.foo                      # => "nikto"
  Child.foo                       # => "barada"

  Child.bar = "klaatu"
  Child.bar                       # => "klaatu"
  Parent.bar                      # =>
  # ~> -:38: undefined method `bar' for Parent:Class (NoMethodError)

RATIONALE:

Clafamatt sprung from a desire to have ActiveSupport-style class inheritable attributes without having all of ActiveSupport come with it. It is also an attempt to implement class-inheritable attributes in the cleanest way possible.

In contrast to the ActiveSupport version, which writes reader/writer methods directly into the class being extended, Clafamatt writes it‘s readers and writers into a dynamically created module which is then inserted into the class’ (or module‘s) singleton class. Among other benefits, this enables Clafamatt to transparently support defining inheritable attributes in either classes or modules.

The ActiveSupport version keeps inheritable attribute values in a class-level @inheritable_attributes variable, which it copies whenever a new class is inherited. Clafamatt stays closer to Ruby conventions by storing each attribute‘s value in a correspondingly-named class instance variable - so e.g. Foo.bar = 42 will set the @bar instance variable. This is consistent with how Ruby‘s built-in attr_* macros behave. Beyond the variables used to store values, Clafamatt needs zero class instance variables for internal bookeeping. It also doesn‘t copy variables on inheritance; instead, it simply re-uses values from farther up the inheritance chain until an attribute is explicitly set.

Finally, Clafamatt is only there when you ask for it, by including Clafamatt::Macros. It will not pollute the global namespace.

REQUIREMENTS:

  • Ruby!

INSTALL:

  sudo gem install clafamatt

LICENSE:

(The MIT License)

Copyright (c) 2008 Avdi Grimm

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.

[Validate]