method visibility

B

Bob Aman

More nubyness. Apologies.

Ok, so, I have this rss feed caching system i'm working on. Ideally,
I'd like to have as little of the internal workings of the thing
visible as possible. I just want the programmer to interact with Feed
and FeedItem objects, not the caching mechanisms.

It seems to me that I don't really want the programmer to be able to
create instances of a FeedItem manually. All FeedItems ought to be
created automatically by a Feed object. But it seems like ruby lacks
the direct equivalent of c++'s "friend", so I guess I can't give
access to the "new" class method only to Feed.

Also, I'd like to make the caching mechanism be changable. It'd be
nice to be able to switch between, say, a database-based cache and a
file system-based cache. Perhaps like this:

FeedCache.cache_type =3D :DatabaseFeedCache

And then the Feed object would simply use the DatabaseFeedCache behind
the scenes. But again, I'm not sure how to make the cache's behavior
available only to the Feed and FeedItem classes (or even if I can).
--=20
Bob Aman
 
R

Robert Klemme

More nubyness. Apologies.

Ok, so, I have this rss feed caching system i'm working on. Ideally,
I'd like to have as little of the internal workings of the thing
visible as possible. I just want the programmer to interact with Feed
and FeedItem objects, not the caching mechanisms.

It seems to me that I don't really want the programmer to be able to
create instances of a FeedItem manually. All FeedItems ought to be
created automatically by a Feed object. But it seems like ruby lacks
the direct equivalent of c++'s "friend", so I guess I can't give
access to the "new" class method only to Feed.

Also, I'd like to make the caching mechanism be changable. It'd be
nice to be able to switch between, say, a database-based cache and a
file system-based cache. Perhaps like this:

FeedCache.cache_type = :DatabaseFeedCache

Why not just assign a proper cache instance to a global variable or to a
feed (whatever is more appropriate)?
And then the Feed object would simply use the DatabaseFeedCache behind
the scenes. But again, I'm not sure how to make the cache's behavior
available only to the Feed and FeedItem classes (or even if I can).

I wouldn't mind too much about the accessibility stuff. If someone wants to
circumvent things this is easy in Ruby so just see that your class design is
appropriate and go ahead. My 0.02 EUR...

Kind regards

robert
 
M

Mark Hubbart

More nubyness. Apologies.
=20
Ok, so, I have this rss feed caching system i'm working on. Ideally,
I'd like to have as little of the internal workings of the thing
visible as possible. I just want the programmer to interact with Feed
and FeedItem objects, not the caching mechanisms.
=20
It seems to me that I don't really want the programmer to be able to
create instances of a FeedItem manually. All FeedItems ought to be
created automatically by a Feed object. But it seems like ruby lacks
the direct equivalent of c++'s "friend", so I guess I can't give
access to the "new" class method only to Feed.

The ruby way, as i would interpret it, would be to make changes to
deter, but not prevent, the user-programmer from doing things you
don't want, and trusting them to take the warning.

For an example of this, take a look at complex.rb and rational.rb in
the stdlib. Both of those classes show different ways of doing this,
like replacing the #new method after aliasing it to #new2. This allows
people to work naturally with the class, and follows the ruby way. And
when you want to create a new version of the class in your code, call
#new2, instead.

cheers,
Mark
 
L

Logan Capaldo

More nubyness. Apologies.
=20
Ok, so, I have this rss feed caching system i'm working on. Ideally,
I'd like to have as little of the internal workings of the thing
visible as possible. I just want the programmer to interact with Feed
and FeedItem objects, not the caching mechanisms.
=20
It seems to me that I don't really want the programmer to be able to
create instances of a FeedItem manually. All FeedItems ought to be
created automatically by a Feed object. But it seems like ruby lacks
the direct equivalent of c++'s "friend", so I guess I can't give
access to the "new" class method only to Feed.
=20
Also, I'd like to make the caching mechanism be changable. It'd be
nice to be able to switch between, say, a database-based cache and a
file system-based cache. Perhaps like this:
=20
FeedCache.cache_type =3D :DatabaseFeedCache
=20
And then the Feed object would simply use the DatabaseFeedCache behind
the scenes. But again, I'm not sure how to make the cache's behavior
available only to the Feed and FeedItem classes (or even if I can).

You could do one of these:

class A
private_class_method :new
end

class FriendOfA
def make_an_A
A.class_eval { new }
end
end

Of course still doesn't stop anyone from just doing a class_eval
themselves, but it does make it slightly more discouraging, and they
definitely won't do it by accident.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top