class SimpleCsv::Transformer
Constants
- DEFAULT_FILENAME
Public Class Methods
new(path, **opts, &block)
click to toggle source
# File lib/simple_csv/transformer.rb, line 5 def initialize(path, **opts, &block) @transforms = {} @output_headers = [] @caller_self = eval 'self', block.binding if settings.for_csv[:headers] @csv_path = File.expand_path path headers(*find_headers) unless headers.any? end instance_exec(self, &block) apply_transforms path, **opts end
Public Instance Methods
output_headers(*out_headers)
click to toggle source
# File lib/simple_csv/transformer.rb, line 21 def output_headers(*out_headers) return @output_headers if @output_headers.any? @output_headers = out_headers.map(&:to_s) alias_to_friendly_headers @output_headers @output_headers end
Private Instance Methods
apply_transforms(path, **opts)
click to toggle source
# File lib/simple_csv/transformer.rb, line 31 def apply_transforms(path, **opts) received_headers = headers timestamp = Time.new.strftime '%d-%m-%Y-%S%7N' output_path = opts.delete(:output) || "#{path.split('.')[0..-2].join}-#{timestamp}.csv" output_headers = @output_headers.any? ? @output_headers : received_headers SimpleCsv.read path, opts do |reader| SimpleCsv.generate output_path, opts do |writer| writer.headers *output_headers reader.each_row do output_headers.each do |column| transform = find_transform column result = transform ? transform.call(reader.send(column)) : reader.send(column) writer.send column, result end end end end end
find_transform(column)
click to toggle source
# File lib/simple_csv/transformer.rb, line 54 def find_transform(column) @transforms[(@col_map.key(column.to_s) || column).to_sym] end
method_missing(mtd, *args, &block)
click to toggle source
# File lib/simple_csv/transformer.rb, line 58 def method_missing(mtd, *args, &block) mstr = mtd.to_s if headers.include?(mstr) || @output_headers.include?(mstr) || @col_map.key?(mstr) @transforms[mtd] = block || args.first unless @transforms.key? mtd else @caller_self.send mtd, *args, &block end end