Class: Sequel::Export::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/extensions/export.rb

Instance Method Summary collapse

Constructor Details

#initialize(fd, dataset, options) ⇒ Writer

Returns a new instance of Writer.



32
33
34
35
36
# File 'lib/extensions/export.rb', line 32

def initialize(fd, dataset, options)
  @file = fd
  @dataset = dataset
  @options = options
end

Instance Method Details

#build_row(row) ⇒ Object



80
81
82
83
# File 'lib/extensions/export.rb', line 80

def build_row(row) 
  quot = @options[:quote_char] 
  @columns.map{|col| row[col].to_export(quot)}.join(@options[:delimiter])
end

#export_data(ds) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/extensions/export.rb', line 38

def export_data(ds)
  quot = @options[:quote_char]
  ds.each do |row| 
    data = @columns.map do |col|
      case row[col]
      when Date then 
        "#{quot}#{row[col].strftime('%Y-%m-%d')}#{quot}"
      when DateTime then
        "#{quot}#{row[col].localtime.strftime('%Y-%m-%dT%H:%M%Z')}#{quot}"
      when Time then 
        "#{quot}#{row[col].localtime.strftime('%H:%M%Z')}#{quot}"
      when Float, BigDecimal then 
        row[col].to_f
      when BigDecimal, Bignum, Fixnum then 
        row[col].to_i
      else 
        "#{quot}#{row[col].to_s}#{quot}"
      end
    end
    @file.puts data.join(@options[:delimiter])
  end
end

#outputObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/extensions/export.rb', line 61

def output 
  first_row = @dataset.first
  return unless first_row
  
  quot = @options[:quote_char]
  @columns = @dataset.columns
  # @columns = first_row.keys

  if @options[:headers] == true
    @file.puts @columns.map{|col| "#{quot}#{col}#{quot}"}.join(@options[:delimiter])
  end

  if @options[:paginate]
    @dataset.each_page(@options[:page_size]){|paged_ds| export_data paged_ds}
  else
    export_data @dataset
  end
end