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

#initialize(attributes = {}) ⇒ Base

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

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Class Method Details

.allObject

Returns all records imported from the excel document.



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

def self.all
  @records
end

.column(*args) ⇒ Object

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

.import(path, worksheet_index = 0) ⇒ Object

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)
  Ole::Log.level = Logger::ERROR # to avoid the annoying "root name was" warning
  @records ||= []
  workbook = Spreadsheet.open(path)
  worksheet = workbook.worksheet(worksheet_index)
  worksheet.format_dates!
  columns = worksheet.row(0).map do |cell|
    @columns.detect { |column| column.name == cell.to_s }
  end
  (@columns.select(&:required?) - columns).each do |column|
    raise MissingColumn, "Column #{column.name} is required but it doesn't exist."
  end
  (1..worksheet.last_row_index).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 = ""
          else
            value = row.at(index).to_s
          end
          attributes[column.name] = column.cell_value(value, row_number)
        end
      end
      @records << new(attributes)
    end
  end
end

Instance Method Details

#[](name) ⇒ Object

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