Forum: Ruby on Rails Using variable class names, how?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-02-28 22:28
(Received via mailing list)
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
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-02-28 22:43
(Received via mailing list)
Start by reading this ->
http://groups.google.com/group/rubyonrails-talk/br...

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)
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-03-01 15:15
(Received via mailing list)
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
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-03-01 17:08
(Received via mailing list)
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)
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-03-01 17:26
(Received via mailing list)
Hey, no problem, thank you for taking time! :)

-- ms
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-01 17:53
(Received via mailing list)
On Mar 1, 4:26 pm, ms <m...@tzi.de> wrote:
> Hey, no problem, thank you for taking time! :)
>
Is "Foo".constantize what you're looking for ?

Fred
0d56133042e4814c1b2255ea655f2361?d=identicon&s=25 Jeff Burlysystems (jburly)
on 2009-03-01 20:11
(Received via mailing list)
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-01 20:33
(Received via mailing list)
Jeff Lewis 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
0d56133042e4814c1b2255ea655f2361?d=identicon&s=25 Jeff Burlysystems (jburly)
on 2009-03-01 20:43
(Received via mailing list)
My example used clss not class, ... but sure klass if you prefer.

Jeff
This topic is locked and can not be replied to.