lib/marty/data_exporter.rb in marty-0.5.20 vs lib/marty/data_exporter.rb in marty-0.5.21

- old
+ new

@@ -76,19 +76,20 @@ } end end end - def self.export_attrs(klass, obj, attrs=nil) + def self.export_attrs(klass, obj, attrs=nil, exclude_attrs=[]) col_types = Marty::DataConversion.col_types(klass) - (attrs || col_types.keys).map do + attr_list = (attrs || col_types.keys).map(&:to_s) - exclude_attrs + + attr_list.map do |c| v = obj.send(c.to_sym) - c = c.to_s type = col_types[c] # return [value] if not assoc or nil next [v] if v.nil? || !type.is_a?(Hash) @@ -101,17 +102,18 @@ # create the export structure first. export_attrs(assoc_class, assoc_obj, assoc_keys).flatten(1) end end - def self.export_headers(klass, attrs=nil) + def self.export_headers(klass, attrs=nil, exclude_attrs=[]) col_types = Marty::DataConversion.col_types(klass) - (attrs || col_types.keys).map do + attr_list = (attrs || col_types.keys).map(&:to_s) - exclude_attrs + + attr_list.map do |c| - c = c.to_s type = col_types[c] next c unless type.is_a?(Hash) # remove _id @@ -128,25 +130,27 @@ end end # Given a Mcfly klass, generate an export array. Can potentially # use up a lot of memory if the result set is large. - def self.do_export(ts, klass, sort_field=nil) + def self.do_export(ts, klass, sort_field=nil, exclude_attrs=[]) query = klass if Mcfly.has_mcfly?(klass) ts = Mcfly.normalize_infinity(ts) query = query.where("obsoleted_dt >= ? AND created_dt < ?", ts, ts) end - do_export_query_result(klass, query.order(sort_field || :id)) + do_export_query_result(klass, query.order(sort_field || :id), exclude_attrs) end - def self.do_export_query_result(klass, qres) + def self.do_export_query_result(klass, qres, exclude_attrs=[]) # strip _id from assoc fields - header = [ export_headers(klass).flatten ] + header = [ export_headers(klass, nil, exclude_attrs).flatten ] - header + qres.map {|obj| export_attrs(klass, obj).flatten(1)} + header + qres.map {|obj| + export_attrs(klass, obj, nil, exclude_attrs).flatten(1) + } end # Export a single object to hash -- FIXME: inefficient # implementation def self.export_obj(obj)