[ANN] Defaultable 0.0.3

J

John W. Long

This is the first public release of a very small project I've been
working on recently. It's very similar actually to Ara.T.Howard's
implementation of Traits.

The goal of the project is to provide a mixin for objects that adds a
series custom attribute methods for default values:

require 'defaultable'

class TestObject
include Defaultable
default_accessor :test => 'Hello world!'
end

o = TestObject.new
puts o.test #=> 'Hello world!'

If the object's constructor has a hash as the last parameter it assumes
it's an options hash and automatically extracts values for the object
from it:

class TestObject
def initialize(options = {})
end
end

o = TestObject.new:)test => 'Cool beans!')
puts o.test #=> 'Cool beans!'

The current implementation also works well with inheritance allowing
inherited objects to inherit their parent object's attribute defaults.

I'm particularly interested in feedback on the implementation. I'm not a
metaclass guru, so for those of you who are, I would especially
appreciate feedback on the class methods "included" and "method_added"
on the module. These methods are currently hacks to make it useful, but
their implementation isn't pretty.

Download:
http://johnwlong.com/downloads/iblp-mixins-0.0.3/lib/defaultable.rb

Tests:
http://johnwlong.com/downloads/iblp-mixins-0.0.3/test/unit/defaultable_test.rb

Defaultable is currently part of another small project which can be
downloaded here:

http://johnwlong.com/downloads/iblp-mixins-0.0.3.tgz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[ANN] Updateable 0.0.3 1
[ANN] bj-0.0.3 0
[ANN] flatulent-0.0.3 21
[ANN] Net::SSH 0.0.3 18
[ANN] dike-0.0.3 2
[ANN] dirwatch-0.0.3 0
ANN main-4.4.0 0
mixins 2

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top