[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Activerecord associations like class structure

Marcelo Barbudas

5/7/2009 12:46:00 PM

Hi,

This question is based on another thread that helped me realize the
initial idea I had about a library format was not good.

How is it possible to mimic an ActiveRecord association like class
structure?

Something like:
Module Something
Class Main
def connection
end
end

Class Resource < Main
def method
call_resource
end
end
end

and being able to create something like
con = Main.new(user, pass)
result = con.resources.method

Thanks.

Sorry for opening another thread about this.

--
M.

1 Answer

Christopher Dicely

5/7/2009 11:19:00 PM

0

On Thu, May 7, 2009 at 5:46 AM, Marcelo Barbudas <nostef@gmail.com> wrote:
> Hi,
>
> This question is based on another thread that helped me realize the initi=
al
> idea I had about a library format was not good.
>
> How is it possible to mimic an ActiveRecord association like class
> structure?
>
> Something like:
> Module Something
> =C2=A0Class Main
> =C2=A0 def connection
> =C2=A0 end
> =C2=A0end
>
> =C2=A0Class Resource < Main
> =C2=A0 def =C2=A0method
> =C2=A0 =C2=A0 =C2=A0call_resource
> =C2=A0 end
> =C2=A0end
> end
>
> and being able to create something like
> con =3D Main.new(user, pass)
> result =3D con.resources.method
>
> Thanks.
>
> Sorry for opening another thread about this.
>

One way (and one that lets you either use the default connection on
the base class or override it per sub-class) would be something like:

module Generic
class Base
def self.connect(resource, opts)
@connection =3D resource.connect(opts)
end

def connection
@connection || (self!=3DGeneric::Base && Generic::Base.connection)
end
end
end

class Resource < Generic::Base
# whatever
end