Sha256: e19764c327d96ba3e2efc6e4e780f219bdbed8b9dce0e476ed3aab07eacbde9d
Contents?: true
Size: 1.94 KB
Versions: 1
Compression:
Stored size: 1.94 KB
Contents
class ActiveReport::Hash attr_accessor :datum, :only, :except, :headers, :options def initialize(datum, only: nil, except: nil, headers: nil, options: {}) @datum = datum @only = only @except = except @headers = headers @options = options end def self.export(datum, only: nil, except: nil, headers: nil, options: {}) new(datum, only: only, except: except, headers: headers, options: options).export end def self.import(datum, only: nil, except: nil, headers: nil, options: {}) new(datum, only: only, except: except, headers: headers, options: options).import end def export @datum = [].push(@datum).compact unless @datum.is_a?(Array) @only = [].push(@only).compact unless @only.is_a?(Array) @except = [].push(@except).compact unless @except.is_a?(Array) CSV.generate(@options) do |csv| header = @datum.first.only(@only) unless @only.empty? header = @datum.first.except(@except) unless @except.empty? csv << (@headers || (header || @datum.first).keys.map { |k| k.to_s.gsub('_'.freeze, ' '.freeze).capitalize }) @datum.lazy.each do |data| cell = data.only(@only) unless @only.empty? cell = data.except(@except) unless @except.empty? csv << (cell || data).values end end end def import @only = [].push(@only).compact unless @only.is_a?(Array) @except = [].push(@except).compact unless @except.is_a?(Array) processed_datum = [] CSV.foreach(@datum, @options).lazy.each_with_index do |data, line| if @headers.nil? && line.zero? @headers = data else processed_data = {} @headers.lazy.each_with_index { |v, i| processed_data.store(v.to_s, data.fetch(i, nil)) } processed_data.only!(@only) unless @only.empty? processed_data.except!(@except) unless @except.empty? processed_datum.push(processed_data) end end return(processed_datum) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_report-2.3.0 | lib/active_report/hash.rb |