Using variable class names, how?


#1

Hello,

I’ve got some code, which I would like to reuse. The only difference
between the usages are the class names and therefore the different
objects. How would you manage this? Is there a way to take the class
names as strings? Do you know other solutions, a suitable pattern, for
example?

Thanx for any suggestions!

Best regards, ms


#2

Start by reading this ->
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/dce05002d11b4849?hl=en#

The group archive is always a good place to start your search before
sending messages.

Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)


#3

Thanks for your answer, I read the thread, but this did not really
help me. It’s not just about redefining some core methods. I think,
the description of my problem was not clear enough, sorry for this. I
would appreciate, if you could have a look at the following
explanation.

I’ve got several entities saved in my database: E1, E2 … En. All
these entities can be flexibly annotated, so I’ve got the following
models:

E1 (Entity) => The entity itself
EA1 (EntityAnnotation) => The various entity annotations, may be
nested

ET (EntityType) => The used entity type which tells something about
the used datatype, display properties, etc.

This group of models repeat themselves for each exposed entity, so
I’ve got:

E1…n
EA1…n

ET

Now comes the point, I was originally refering to:

I created some code which checks, if the given annotation type can be
applied to the entity, what kind of sub-entities (children) the
annotation might can have and how much of them and then save them to
the entity annotation table. Here’s some pseudo-code for this:

1: fetch all annotations / attributes which should be attached to a
entity (those data comes from html formular)
2: check, whether the given annotations can be applied to the entity
and if they have the correct value type
3: if there are sub-annotations, check whether they are valid and do
not exceed the maximum of allowed children and if they also have the
correct value type
4: (check more various constraints…)
5: (maybe, some more stuff to do…)
6: if there’s no error, save the annotations with the given types and
values

As I want to save alle entites separately for some kind of reason, I
come to this (refering to the pseudo code above):

1: fetch the data
2: validate given annotations regarding E1
3: validate given annotation children regarding E1
4: …
5: …
6: no error: save E1 and all corresponding EA1

For the next entity I would repeat all the stuff, just changing the
class names / object names, but gaining all the logic. The point is,
that I don’t want to use some kind of case distinction directly in the
code.

The class using this modul should just define a string constant
defining which entity class / entity annotation class to use. I just
need a possibility to handle class names as string an vice versa like
I do when using the core “send(meth_name, attr, &block)” method.

I hope, this explanation is adequate.

Thanx for your help!

Best regards, ms


#4

Sorry. but i’m completely clueless. Can’t really understand what
you’re trying to say/do.

Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)


#5

Hey, no problem, thank you for taking time! :slight_smile:

– ms


#6

On Mar 1, 4:26 pm, ms removed_email_address@domain.invalid wrote:

Hey, no problem, thank you for taking time! :slight_smile:

Is “Foo”.constantize what you’re looking for ?

Fred


#7

Instead of passing class names as strings, why not ref the classes
themselves? Lame example, but … something like:


def first_last_count(clss)
return [clss.find(:first), clss.find(:last), clss.count]
end


first_car,last_car,count_cars = first_last_count(Car)

first_bike,last_bike,count_bikes = first_last_count(Bike)

Jeff


#8

My example used clss not class, … but sure klass if you prefer.

Jeff


#9

Jeff L. wrote:

def first_last_count(clss)

Note the tradition there is klass:

http://google.com/codesearch?q=lang%3Aruby+klass

Being ‘class’ is a keyword…


Phlip