[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

method visibility

Bob Aman

5/28/2005 8:21:00 PM

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

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).
--
Bob Aman


3 Answers

Robert Klemme

5/29/2005 3:16:00 PM

0

>
> "Bob Aman" <vacindak@gmail.com> schrieb im Newsbeitrag
> news:599af4e205052813201d9720d8@mail.gmail.com...
> 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

Mark Hubbart

5/29/2005 5:56:00 PM

0

On 5/28/05, Bob Aman <vacindak@gmail.com> wrote:
> 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.

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


Logan Capaldo

5/29/2005 9:03:00 PM

0

On 5/28/05, Bob Aman <vacindak@gmail.com> wrote:
> 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
>
> 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).
> --
> Bob Aman
>
>

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.