lib/dbf/table.rb in dbf-1.6.7 vs lib/dbf/table.rb in dbf-1.7.0
- old
+ new
@@ -27,18 +27,29 @@
attr_reader :version # Internal dBase version number
attr_reader :record_count # Total number of records
attr_accessor :encoding # Source encoding (for ex. :cp1251)
# Opens a DBF::Table
- # Example:
+ # Examples:
+ # # working with a file stored on the filesystem
# table = DBF::Table.new 'data.dbf'
#
- # @param [String] path Path to the dbf file
- def initialize(path)
- @data = File.open(path, 'rb')
+ # # working with a misnamed memo file
+ # table = DBF::Table.new 'data.dbf', 'memo.dbt'
+ #
+ # # working with a dbf in memory
+ # table = DBF::Table.new StringIO.new(dbf_data)
+ #
+ # # working with a dbf and memo in memory
+ # table = DBF::Table.new StringIO.new(dbf_data), StringIO.new(memo_data)
+ #
+ # @param [String, StringIO] data Path to the dbf file or a StringIO object
+ # @param [optional String, StringIO] memo Path to the memo file or a StringIO object
+ def initialize(data, memo = nil)
+ @data = open_data(data)
get_header_info
- @memo = open_memo(path)
+ @memo = open_memo(data, memo)
end
# @return [TrueClass, FalseClass]
def has_memo_file?
!!@memo
@@ -119,16 +130,15 @@
end
# Dumps all records to a CSV file. If no filename is given then CSV is
# output to STDOUT.
#
- # @param [optional String] path Defaults to basename of dbf file
+ # @param [optional String] path Defaults to STDOUT
def to_csv(path = nil)
- csv_class.open(path || default_csv_path, 'w', :force_quotes => true) do |csv|
- csv << columns.map {|c| c.name}
- each {|record| csv << record.to_a}
- end
+ csv = csv_class.new((path ? File.open(path, 'w') : $stdout), :force_quotes => true)
+ csv << columns.map {|c| c.name}
+ each {|record| csv << record.to_a}
end
# Find records using a simple ActiveRecord-like syntax.
#
# Examples:
@@ -197,17 +207,29 @@
def column_count #nodoc
@column_count ||= (@header_length - DBF_HEADER_SIZE + 1) / DBF_HEADER_SIZE
end
- def open_memo(path) #nodoc
- dirname = File.dirname(path)
- basename = File.basename(path, '.*')
- files = Dir.glob("#{dirname}/#{basename}*.{fpt,FPT,dbt,DBT}")
- files.any? ? Memo.open(files.first, version) : nil
+ def open_data(data)
+ data.is_a?(StringIO) ? data : File.open(data, 'rb')
end
+ def open_memo(data, memo = nil) #nodoc
+ if memo.is_a? StringIO
+ DBF::Memo.new(memo, version)
+ elsif memo
+ DBF::Memo.open(memo, version)
+ elsif !data.is_a? StringIO
+ dirname = File.dirname(data)
+ basename = File.basename(data, '.*')
+ files = Dir.glob("#{dirname}/#{basename}*.{fpt,FPT,dbt,DBT}")
+ files.any? ? DBF::Memo.open(files.first, version) : nil
+ else
+ nil
+ end
+ end
+
def find_all(options) #nodoc
map do |record|
if record.match? options
yield record if block_given?
record
@@ -234,13 +256,9 @@
@data.seek @header_length + offset
end
def csv_class #nodoc
CSV.const_defined?(:Reader) ? FCSV : CSV
- end
-
- def default_csv_path #nodoc
- File.basename(@data.path, '.dbf') + '.csv'
end
def self.encodings #nodoc
@encodings ||= YAML.load_file File.expand_path("../encodings.yml", __FILE__)
end