Howdy.
I''ve got an overridden to_xml() function in one of my models that
appears to be very slow. As a relative Ruby novice, I would love to
hear suggestions on how to improve the performance. Incidentally, I
was using the default to_xml() provided by ActiveRecord, but it wasn''t
handling null data the way I wanted.
class Errors < ActiveRecord:Base
...
def to_xml(options = {})
options[:indent] ||= 2
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent =>
options[:indent])
xml.instruct! unless options[:skip_instruct]
xml.error do |x|
x.id(self.id) if self.attributes.has_key?
("id")
x.division_name(self.division_name) if self.attributes.has_key?
("division_name")
x.division_id(self.division_id) if self.attributes.has_key?
("division_id")
x.region_name(self.region_name) if self.attributes.has_key?
("region_name")
x.region_id(self.region_id) if self.attributes.has_key?
("region_id")
x.market_name(self.market_name) if self.attributes.has_key?
("market_name")
x.market_id(self.market_id) if self.attributes.has_key?
("market_id")
x.vp_name(self.vp_name) if self.attributes.has_key?
("vp_name")
x.vp_id(self.vp_id) if self.attributes.has_key?
("vp_id")
x.dir_name(self.dir_name) if self.attributes.has_key?
("dir_name")
x.dir_id(self.dir_id) if self.attributes.has_key?
("dir_id")
x.mgr_name(self.mgr_name) if self.attributes.has_key?
("mgr_name")
x.mgr_id(self.mgr_id) if self.attributes.has_key?
("mgr_id")
x.sup_name(self.sup_name) if self.attributes.has_key?
("sup_name")
x.sup_id(self.sup_id) if self.attributes.has_key?
("sup_id")
x.cae_name(self.cae_name) if self.attributes.has_key?
("cae_name")
x.cae_id(self.cae_id) if self.attributes.has_key?
("cae_id")
x.acct(self.acct) if self.attributes.has_key?
("acct")
x.wono(self.wono) if self.attributes.has_key?
("wono")
# x.crtd(self.crtd.strftime("%Y-%m-%d")) # Why wouldn''t
this
have worked?...
# perhaps it should have been crtd.to_date.strftime() ...
# x.schd(self.schd.strftime("%Y-%m-%d"))
x.crtd(self.crtd ? self.crtd.to_date.to_s(:db) : "")
if self.attributes.has_key?("crtd")
x.schd(self.schd ? self.schd.to_date.to_s(:db) : "")
if self.attributes.has_key?("schd")
x.run_date(self.run_date ? self.run_date.to_date.to_s(:db) : "")
if self.attributes.has_key?("run_date")
x.errtype(self.errtype) if self.attributes.has_key?
("errtype")
x.waiver(self.waiver) if self.attributes.has_key?
("waiver")
x.stream(self.stream) if self.attributes.has_key?
("stream")
x.disposition(self.disposition) if self.attributes.has_key?
("disposition")
x.biller(self.biller) if self.attributes.has_key?
("biller")
x.corp(self.corp) if self.attributes.has_key?
("corp")
x.alert(self.alert) if self.attributes.has_key?
("alert")
x.audit_classification(self.audit_classification) if
self.attributes.has_key?("audit_classification")
x.audit_category(self.audit_category) if
self.attributes.has_key?("audit_category")
end
end
...
end
THANKS!
Frederick Cheung
2009-Jul-09 20:30 UTC
Re: seeking optimization help - overridden to_xml()
On Jul 9, 8:20 pm, michael_teter <michael.te...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Howdy. > > I''ve got an overridden to_xml() function in one of my models that > appears to be very slow. As a relative Ruby novice, I would love to > hear suggestions on how to improve the performance. Incidentally, I > was using the default to_xml() provided by ActiveRecord, but it wasn''t > handling null data the way I wanted. >The attributes method returns a new hash everytime you call it - it might help to only call it once and stash the result in a local variable. You might find the :only option to to_xml allows you to solve this problem without all this rather repetitive code. Fred> class Errors < ActiveRecord:Base > ... > def to_xml(options = {}) > options[:indent] ||= 2 > xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => > options[:indent]) > xml.instruct! unless options[:skip_instruct] > xml.error do |x| > x.id(self.id) if self.attributes.has_key? > ("id") > x.division_name(self.division_name) if self.attributes.has_key? > ("division_name") > x.division_id(self.division_id) if self.attributes.has_key? > ("division_id") > x.region_name(self.region_name) if self.attributes.has_key? > ("region_name") > x.region_id(self.region_id) if self.attributes.has_key? > ("region_id") > x.market_name(self.market_name) if self.attributes.has_key? > ("market_name") > x.market_id(self.market_id) if self.attributes.has_key? > ("market_id") > x.vp_name(self.vp_name) if self.attributes.has_key? > ("vp_name") > x.vp_id(self.vp_id) if self.attributes.has_key? > ("vp_id") > x.dir_name(self.dir_name) if self.attributes.has_key? > ("dir_name") > x.dir_id(self.dir_id) if self.attributes.has_key? > ("dir_id") > x.mgr_name(self.mgr_name) if self.attributes.has_key? > ("mgr_name") > x.mgr_id(self.mgr_id) if self.attributes.has_key? > ("mgr_id") > x.sup_name(self.sup_name) if self.attributes.has_key? > ("sup_name") > x.sup_id(self.sup_id) if self.attributes.has_key? > ("sup_id") > x.cae_name(self.cae_name) if self.attributes.has_key? > ("cae_name") > x.cae_id(self.cae_id) if self.attributes.has_key? > ("cae_id") > x.acct(self.acct) if self.attributes.has_key? > ("acct") > x.wono(self.wono) if self.attributes.has_key? > ("wono") > # x.crtd(self.crtd.strftime("%Y-%m-%d")) # Why wouldn''t this > have worked?... > # perhaps it should have been crtd.to_date.strftime() ... > # x.schd(self.schd.strftime("%Y-%m-%d")) > x.crtd(self.crtd ? self.crtd.to_date.to_s(:db) : "") > if self.attributes.has_key?("crtd") > x.schd(self.schd ? self.schd.to_date.to_s(:db) : "") > if self.attributes.has_key?("schd") > x.run_date(self.run_date ? self.run_date.to_date.to_s(:db) : "") > if self.attributes.has_key?("run_date") > x.errtype(self.errtype) if self.attributes.has_key? > ("errtype") > x.waiver(self.waiver) if self.attributes.has_key? > ("waiver") > x.stream(self.stream) if self.attributes.has_key? > ("stream") > x.disposition(self.disposition) if self.attributes.has_key? > ("disposition") > x.biller(self.biller) if self.attributes.has_key? > ("biller") > x.corp(self.corp) if self.attributes.has_key? > ("corp") > x.alert(self.alert) if self.attributes.has_key? > ("alert") > x.audit_classification(self.audit_classification) if > self.attributes.has_key?("audit_classification") > x.audit_category(self.audit_category) if > self.attributes.has_key?("audit_category") > end > end > ... > end > > THANKS!