Forum: Ruby on Rails Including common code among multiple web services issue?

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.
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-05-08 22:02
(Received via mailing list)
Hi all -

I'm implementing some web services and every one has at the very least a
"find_newer_than" method.

In my models, I've split things up like this and it works great.

----------------------------------------------------------------------------------
class XyzService < WebService
   web_service_api XyzApi
.... service specific methods go here...
end

class WebService < ActionWebService::Base
   def find_newer_than(created_at)
     real_class.find(:all,
                     :conditions => ['created_at >= ?',
TimeZone['London'].unadjust(created_at)])
   end
...
   def real_class
     Class.const_get(self.class.to_s.gsub(/Service$/, ''))
   end
...
end
----------------------------------------------------------------------------------

I'd like to do the same for the API's, but can't quite get there.  If I
try the same logic as above then when I go to /api/invoke all the
methods show up as being of the "WebService" class.

Right now I've got this and it works.  I'd like to move that api_method
line into a common file though.  Since subclassing the Api's another
level didn't work I've tried both require and load, but require doesn't
work at all, and load gives me an unknown method error for
Object#api_method.

----------------------------------------------------------------------------------
class XyzApi < ActionWebService::API::Base

   api_method :find_newer_than,
     :expects => [{:created_at => :datetime}],
     :returns => [[Class.const_get(self.name.to_s.gsub(/Api$/, ''))]]
...
end
----------------------------------------------------------------------------------


So, what I really want is to move that code into it's own file and get
ruby to simply *insert* it as is into each of my files and then parse
it.

Similar to PHP's include() function.

Suggestions?

THanks!

-philip
This topic is locked and can not be replied to.