Convenience method for providing user-definable callbacks
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
MIT-LICENSE
README.rdoc
Rakefile
attr_callback.gemspec

README.rdoc

attr_callback - Convenience method for providing user-definable callbacks.

The attr_callback gem lets you conveniently create user-definable callback method attributes, using a single line of code.

Example Usage

You can use attr_callback to define a callback attribute on an object:

require 'attr_callback'

class Foo
  attr_callback :on_receive_message
end

f = Foo.new

# Getter and setter behave the same as they would using attr_attribute
f.on_receive_message = 1                           #=> 1
f.on_receive_message                               #=> 1

# Passing a block to the getter makes it behave as a setter
f.on_receive_message { |msg| puts msg }            #=> #<Proc:0x0006bfd0@(irb):1>
f.on_receive_message                               #=> #<Proc:0x0006bfd0@(irb):1>

# Invoking the callback
f.on_receive_message.call("hello world")

Options

By default, a Mutex object will be used to guard access to the callback. This Mutex is created the first time the attribute is accessed. Use :lock=>false to disable this behaviour.

By default, a no-op Proc is returned by the getter in place of nil. Use :noop=>false to disable this behaviour.

License

Copyright © 2010 Infonium Inc.

License: See the MIT-LICENSE file.