Class: SimpleCsv::Transformer

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_csv/transformer.rb

Constant Summary collapse

DEFAULT_FILENAME =
'converted.csv'

Constants inherited from Base

Base::COMMON_DELIMITERS

Instance Attribute Summary

Attributes inherited from Base

#index

Instance Method Summary collapse

Constructor Details

#initialize(path, **opts, &block) ⇒ Transformer

Returns a new instance of Transformer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object (private)



58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#output_headers(*out_headers) ⇒ Object



21
22
23
24
25
26
27
# 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