Sha256: b66ee81a8c084a8a0a87337aaf08e0a871e54d09fbe0f0847169ce91cf9119a7
Contents?: true
Size: 1.4 KB
Versions: 3
Compression:
Stored size: 1.4 KB
Contents
# frozen_string_literal: true class ActiveReport::Hash < ActiveReport::Base attr_accessor :datum, :only, :except, :headers, :options def initialize(datum, only: nil, except: nil, headers: nil, options: {}) @datum = datum @only = munge(only) @except = munge(except) @headers = headers @options = csv_options.merge(options) end def self.export(datum, only: nil, except: nil, headers: nil, options: {}) klass = new(datum, only: only, except: except, headers: headers, options: options) klass.export end def self.import(datum, only: nil, except: nil, headers: nil, options: {}) klass = new(datum, only: only, except: except, headers: headers, options: options) klass.import end def export @datum = munge(@datum) CSV.generate(@options) do |csv| csv << (@headers || filter_humanize_keys(@datum)) @datum.lazy.each { |data| csv << filter_values(data) } end end def import array = [] line = 0 CSV.foreach(@datum, @options) do |data| data = encode_to_utf8(data) if csv_force_encoding? if @headers.nil? && line.zero? @headers = data else subdata = {} @headers.lazy.each_with_index { |header, idx| subdata[header.to_s] = data[idx] } filter(subdata) array.push(subdata) end line += 1 end array = array.first if array.size == 1 metatransform(array) end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
active_report-5.1.0 | lib/active_report/hash.rb |
active_report-5.0.1 | lib/active_report/hash.rb |
active_report-5.0.0 | lib/active_report/hash.rb |