adding class-methods via modules

J

John Wilger

Hello,

I have a module which, when a class includes the module, I would like
to have it add certain class-methods to the including class.
Currently, I am doing the following:

module Foo
def self.included(mod)
mod.class_eval <<-EOF, __FILE__, __LINE__+1
def self.bar
...
end
EOF
end
end

class Baz
include Foo
end

This does accomplish the end goal---Baz::bar is a valid class
method---but it has the downside that RDoc doesn't pick up the class
methods defined inside Foo::included for documentation purposes.

Is there either a) a way to get RDoc to pick up documentation for the
::bar method (and simply document it as Foo::bar), or b) a better way
of creating these class methods on classes that include Foo that will
also allow RDoc to document these methods?

--
Regards,
John Wilger

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland
 
R

Robert Klemme

John Wilger said:
Hello,

I have a module which, when a class includes the module, I would like
to have it add certain class-methods to the including class.
Currently, I am doing the following:

module Foo
def self.included(mod)
mod.class_eval <<-EOF, __FILE__, __LINE__+1
def self.bar
...
end
EOF
end
end

class Baz
include Foo
end

This does accomplish the end goal---Baz::bar is a valid class
method---but it has the downside that RDoc doesn't pick up the class
methods defined inside Foo::included for documentation purposes.

Is there either a) a way to get RDoc to pick up documentation for the
::bar method (and simply document it as Foo::bar), or b) a better way
of creating these class methods on classes that include Foo that will
also allow RDoc to document these methods?

Two options for b) come to mind:

# option 1
module Foo
module FooClassMethods
def bar() puts "#bar#" end
end

def self.included(mod)
class <<mod
include FooClassMethods
end
end
end

# option 2
module Foo2
def self.included(mod)
class <<mod
def bar2() puts "#bar2#" end
end
end
end

class Baz
include Foo
include Foo2
end

Baz.bar
Baz.bar2

Personally I'd prefer option 1, because you end up with less method
definitions and if you change definitions in FooClassMethods later all
classes that included Foo will use the new definitions.

Kind regards

robert
 
J

John Wilger

Awesome! This is exactly why I love Ruby---not just the flexibility of
the language, but also the helpfulness of the community. Thanks!

--
Regards,
John Wilger

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland
 
R

Robert Klemme

John Wilger said:
Awesome! This is exactly why I love Ruby---not just the flexibility of
the language, but also the helpfulness of the community. Thanks!

You're welcome.

Btw: option 1 can be written even more concise:

module Foo
module FooClassMethods
def bar() puts "#bar#" end
end

def self.included(mod)
mod.extend FooClassMethods
end
end

class Baz
include Foo
end

Baz.bar

:))

Kind regards

robert
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top