Class: Importex::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/importex/base.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Base) initialize(attributes = {})

Returns a new instance of Base



67
68
69
# File 'lib/importex/base.rb', line 67

def initialize(attributes = {})
  @attributes = attributes
end

Instance Attribute Details

- (Object) attributes (readonly)

Returns the value of attribute attributes



3
4
5
# File 'lib/importex/base.rb', line 3

def attributes
  @attributes
end

Class Method Details

+ (Object) all

Returns all records imported from the excel document.



63
64
65
# File 'lib/importex/base.rb', line 63

def self.all
  @records
end

+ (Object) column(*args)

Defines a column that may be found in the excel document. The first argument is a string representing the name of the column. The second argument is a hash of options.

Options:

:type

The Ruby class to be used as the value on import.

column :type => Date
:format

Usually a regular expression representing the required format for the value. Can also be a string or an array of strings and regular expressions.

column :format => [/^\d+$/, "0.0"]
:required

Boolean specifying whether or not the given column must be present in the Excel document. Defaults to false.



23
24
25
26
# File 'lib/importex/base.rb', line 23

def self.column(*args)
  @columns ||= []
  @columns << Column.new(*args)
end

+ (Object) import(path, worksheet_index = 0)

Pass a path to an Excel (xls) document and optionally the worksheet index. The worksheet will default to the first one (0). The first row in the Excel document should be the column names, all rows after that should be records.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/importex/base.rb', line 31

def self.import(path, worksheet_index = 0)
  @records ||= []
  workbook = Spreadsheet::ParseExcel.parse(path)
  worksheet = workbook.worksheet(worksheet_index)
  columns = worksheet.row(0).map do |cell|
    @columns.detect { |column| column.name == cell.to_s('latin1') }
  end
  (@columns.select(&:required?) - columns).each do |column|
    raise MissingColumn, "Column #{column.name} is required but it doesn't exist."
  end
  (1...worksheet.num_rows).each do |row_number|
    row = worksheet.row(row_number)
    unless row.at(0).nil?
      attributes = {}
      columns.each_with_index do |column, index|
        if column
          if row.at(index).nil?
            value = ""
          elsif row.at(index).type == :date
            value = row.at(index).date.strftime("%Y-%m-%d %H:%M:%I")
          else
            value = row.at(index).to_s('latin1')
          end
          attributes[column.name] = column.cell_value(value, row_number)
        end
      end
      @records << new(attributes)
    end
  end
end

Instance Method Details

- (Object) [](name)

A convenient way to access the column data for a given record.

product["Price"]


75
76
77
# File 'lib/importex/base.rb', line 75

def [](name)
  @attributes[name]
end