When creating a base objects for a remote API i wanted to set the headers on the base object and propagate those to the objects that extend it. Say you want to import all of googles database (not probable ;))
class GoogleObject << ActiveResource::Base
self.headers["User-Agent"] = "My first program"
self.site = "http://www.google.com/"
end
Now we want to get the webpages, we define:
class WebPage << GoogleObject
self.element_name = 'webpages'
end
Now when you perform WebPage.find(:all) The site is correct, but the headers are empty.
I used the following fix:
def headers
if defined?(@headers)
@headers
elsif superclass != Object and superclass.headers
superclass.headers
else
@headers = {}
end
end
def headers=(headers)
@headers = headers
end
Once again it's quite ugly, but it does work :)