Browse files

adding some inline documentation

  • Loading branch information...
1 parent 5cc2293 commit f6c6661caa35da5904720b370c62a8940ae7d170 @ryanb committed Feb 8, 2010
Showing with 32 additions and 0 deletions.
  1. +6 −0 lib/importex.rb
  2. +26 −0 lib/importex/base.rb
View
6 lib/importex.rb
@@ -5,7 +5,13 @@
require File.expand_path(File.dirname(__FILE__) + '/importex/ruby_additions')
module Importex
+ # This is an abstract exception for errors occurred during import. It is recommended
+ # that you rescue from this and handle importe errors in a clean way.
class ImportError < StandardError; end
+
+ # This exception is raised when a cell does not fit the column's :format specification.
class InvalidCell < ImportError; end
+
+ # This exception is raised when a :required column is missing.
class MissingColumn < ImportError; end
end
View
26 lib/importex/base.rb
@@ -2,11 +2,32 @@ module Importex
class Base
attr_reader :attributes
+ # 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.
def self.column(*args)
@columns ||= []
@columns << Column.new(*args)
end
+ # 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.
def self.import(path, worksheet_index = 0)
@records ||= []
workbook = Spreadsheet::ParseExcel.parse(path)
@@ -38,6 +59,7 @@ def self.import(path, worksheet_index = 0)
end
end
+ # Returns all records imported from the excel document.
def self.all
@records
end
@@ -46,6 +68,10 @@ def initialize(attributes = {})
@attributes = attributes
end
+ # A convenient way to access the column data for a given record.
+ #
+ # product["Price"]
+ #
def [](name)
@attributes[name]
end

0 comments on commit f6c6661

Please sign in to comment.