lib/marty/data_exporter.rb in marty-2.5.2 vs lib/marty/data_exporter.rb in marty-2.5.4

- old
+ new

@@ -16,38 +16,38 @@ # # [["a", "b", "c"], [1, 2, nil], [11, nil, 33], [1111, 222, 333]] keys = hash_array_keys(hl) - return keys.each_with_object({}) { |k, rh| + return keys.each_with_object({}) do |k, rh| rh[k] = hl.map { |h| h[k] } - } if transpose + end if transpose - [keys.to_a] + hl.map {|h| keys.map {|k| h[k]}} + [keys.to_a] + hl.map { |h| keys.map { |k| h[k] } } end def self.encode_json(s) Base64.strict_encode64 Zlib.deflate(s) end def self.decode_json(s) Zlib.inflate Base64.strict_decode64(s) end - def self.to_csv(obj, config=nil) + def self.to_csv(obj, config = nil) obj = [obj] unless obj.respond_to? :map config ||= {} # if all array items are hashes, we merge them - obj = hash_array_merge(obj, config["transpose"]) if - obj.is_a?(Array) && obj.all? {|x| x.is_a? Hash} + obj = hash_array_merge(obj, config['transpose']) if + obj.is_a?(Array) && obj.all? { |x| x.is_a? Hash } # symbolize config keys as expected by CSV.generate - conf = config.each_with_object({}) { |(k,v), h| - h[k.to_sym] = v unless k.to_s == "transpose" - } + conf = config.each_with_object({}) do |(k, v), h| + h[k.to_sym] = v unless k.to_s == 'transpose' + end # FIXME: very hacky to default row_sep to CRLF conf[:row_sep] ||= "\r\n" # remove non CSV.generate options before entering generate blocks @@ -67,46 +67,45 @@ end if obj.is_a?(Hash) CSV.generate(conf) do |csv| obj.each do |x| x = [x] unless x.respond_to? :map - csv << x.map { |v| + csv << x.map do |v| case v when Array, Hash readable ? v.to_json : encode_json(v.to_json) when nil nil else v.to_s end - } + end end end end def self.get_attrs_in_order(klass, attrs) return attrs unless klass.const_defined?(:EXPORT_ORDER) + klass::EXPORT_ORDER.select { |attr| attrs.include?(attr) } end - def self.export_attrs(klass, obj, attrs=nil, exclude_attrs=[]) + def self.export_attrs(klass, obj, attrs = nil, exclude_attrs = []) col_types = Marty::DataConversion.col_types(klass) attr_list_raw = (attrs || col_types.keys).map(&:to_s) - exclude_attrs attr_list = get_attrs_in_order(klass, attr_list_raw) - attr_list.map do - |c| - + attr_list.map do |c| v = obj.send(c.to_sym) type = col_types[c] # return [value] if not assoc or nil next [v] if !type.is_a?(Hash) # no child row, return nils for each field - next [nil]*type[:assoc_keys].count if v.nil? + next [nil] * type[:assoc_keys].count if v.nil? assoc_keys = type[:assoc_keys] assoc_class = type[:assoc_class] assoc_obj = assoc_class.find(v) @@ -115,19 +114,17 @@ # create the export structure first. export_attrs(assoc_class, assoc_obj, assoc_keys).flatten(1) end end - def self.export_headers(klass, attrs=nil, exclude_attrs=[]) + def self.export_headers(klass, attrs = nil, exclude_attrs = []) col_types = Marty::DataConversion.col_types(klass) attr_list_raw = (attrs || col_types.keys).map(&:to_s) - exclude_attrs attr_list = get_attrs_in_order(klass, attr_list_raw) - attr_list.map do - |c| - + attr_list.map do |c| type = col_types[c] next c unless type.is_a?(Hash) # remove _id @@ -137,40 +134,40 @@ # if association has a single key, just use col name next c if assoc_keys.length == 1 assoc_class = type[:assoc_class] - export_headers(assoc_class, assoc_keys).map {|k| "#{c}__#{k}"} + export_headers(assoc_class, assoc_keys).map { |k| "#{c}__#{k}" } 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, exclude_attrs=[]) + 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) + query = query.where('obsoleted_dt >= ? AND created_dt < ?', ts, ts) end do_export_query_result(klass, query.order(sort_field || :id), exclude_attrs) end - def self.do_export_query_result(klass, qres, exclude_attrs=[]) + def self.do_export_query_result(klass, qres, exclude_attrs = []) # strip _id from assoc fields - header = [ export_headers(klass, nil, exclude_attrs).flatten ] + header = [export_headers(klass, nil, exclude_attrs).flatten] - header + qres.map {|obj| + header + qres.map do |obj| export_attrs(klass, obj, nil, exclude_attrs).flatten(1) - } + end end # Export a single object to hash -- FIXME: inefficient # implementation def self.export_obj(obj) klass = obj.class headers = export_headers(klass) rec = export_attrs(klass, obj).flatten - Hash[ headers.zip(rec) ] + Hash[headers.zip(rec)] end end