Giter Site home page Giter Site logo

avdi / clafamatt Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 124 KB

CLAss FAMily ATTributes gives your classes class-inheritable attributes without all that tedious mucking about with @inheritable_attributes

Home Page: http://clafamatt.rubyforge.org

Ruby 100.00%

clafamatt's Introduction

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 © 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.

clafamatt's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.