I am trying to write a small library and have the following situation:
class Main
//initializes connection
def request(blabla)
end
end
class Resource < Main
//different API calls
def call
//bla bla
request(call)
end
end
I would like to let the user do something like the AWS library:
Library::Main.initialize_connection(data) // initializes everything
and then just:
Library::Resource.method // and this should work
In Main I have the function that fills the data, I just don't know how to implement the Resource class.
I am trying to write a small library and have the following situation:
class Main
//initializes connection
def request(blabla)
end
end
class Resource < Main
//different API calls
def call
//bla bla
request(call)
end
end
I would like to let the user do something like the AWS library:
Library::Main.initialize_connection(data) // initializes everything
and then just:
Library::Resource.method // and this should work
In Main I have the function that fills the data, I just don't know how
to implement the Resource class.
I'm not sure what your issue is, so let's start with this:
module Library
class Main
def Main.initialize_stuff
puts "initializing"
end
end
class Resource
def Resource.meth
puts "executing meth"
end
end
That doesn't work. When you write a method call without an object in
front of it:
class SpecializedResource < BaseResource
def SpecializedResource.meth
do_stuff #<-------****
end
end
the implicit caller is self. Inside meth, self is equal to the object
that called meth. The object that called meth can be seen in this line:
Library::SpecializedResource.meth
It's the class Library::SpecializedResource. So inside meth, self is
equal to the Library::SpecializedResource class. And the
Library:SpecializedResource class does not have a method named
do_stuff--only instances of that class have a method named
do_stuff(which is inherited from BaseResource). To call the instance
method, you need an instance. So you can do something like this:
class SpecializedResource < BaseResource
def SpecializedResource.meth
sr = SpecializedResource.new
sr.do_stuff
end
end
I do not have insights into AWS beyond what can be gatherere from http://amazon.rubyforge.org/\. I don't find this pattern very
appealing. For something like this to work you need to resort to some
form of global state (global variable, constant, class instance
variable, thread local variable) which means there is potential for
problems when working with multiple threads. Another issue is that it
is not obvious, i.e. you do not see that there is something
transported behind the scenes.
I would at least refrain from placing this pattern in a library unless
it is thread safe and it is sure that there will and can never be the
need for more than a single connection which seems doubtful in case of
AWS.
My 0.02EUR.
Kind regards
robert
···
2009/5/4 Marcelo Barbudas <nostef@gmail.com>:
Hi,
class SpecializedResource < BaseResource
def SpecializedResource.meth
sr = SpecializedResource.new
sr.do_stuff
end
end
This seems a little complicated, not very DRY to write this code for every
method.
How does AWS do it?
AWS::S3::Base.establish_connection!()
and then just:
Bucket.create('jukebox')
I do not have insights into AWS beyond what can be gatherere from http://amazon.rubyforge.org/\. I don't find this pattern very
appealing. For something like this to work you need to resort to some
form of global state (global variable, constant, class instance
variable, thread local variable) which means there is potential for
problems when working with multiple threads. Another issue is that it
is not obvious, i.e. you do not see that there is something
transported behind the scenes.
I would at least refrain from placing this pattern in a library unless
it is thread safe and it is sure that there will and can never be the
need for more than a single connection which seems doubtful in case of
AWS.
I understand. How do you mimic an ActiveResource like structure then?
Main.connect(user, pass) do |conn|
conn.resources.method_from_resource_class_that_uses_main_for
the_actual_request_method
end
Cheers
robert
···
2009/5/5 Marcelo Barbudas <nostef@gmail.com>:
I do not have insights into AWS beyond what can be gatherere from http://amazon.rubyforge.org/\. I don't find this pattern very
appealing. For something like this to work you need to resort to some
form of global state (global variable, constant, class instance
variable, thread local variable) which means there is potential for
problems when working with multiple threads. Another issue is that it
is not obvious, i.e. you do not see that there is something
transported behind the scenes.
I would at least refrain from placing this pattern in a library unless
it is thread safe and it is sure that there will and can never be the
need for more than a single connection which seems doubtful in case of
AWS.
I understand. How do you mimic an ActiveResource like structure then?