lib/dbf/table.rb in dbf-1.6.1 vs lib/dbf/table.rb in dbf-1.6.2
- old
+ new
@@ -34,16 +34,28 @@
def initialize(path)
@data = File.open(path, 'rb')
get_header_info
@memo = open_memo(path)
end
+
+ def has_memo_file?
+ @memo && memo_file_format
+ end
+
+ def memo_file_format
+ @memo.format if has_memo_file?
+ end
# Closes the table and memo file
def close
@memo && @memo.close
@data.close
end
+
+ def filename
+ File.basename @data.path
+ end
# Calls block once for each record in the table. The record may be nil
# if the record has been marked as deleted.
#
# @yield [nil, DBF::Record]
@@ -162,21 +174,20 @@
columns << Column.new(name.strip, type, length, decimal, @encoding) if length > 0
end
columns
end
end
+
+ def supports_encoding?
+ "".respond_to? :encoding
+ end
private
def open_memo(path) #nodoc
- %w(fpt FPT dbt DBT).each do |extname|
- filename = path.sub(/#{File.extname(path)[1..-1]}$/, extname)
- if File.exists?(filename)
- return Memo.open(filename, version)
- end
- end
- nil
+ files = Dir.glob("#{File.dirname(path)}/#{File.basename(path, '.*')}*.{fpt,FPT,dbt,DBT}")
+ files.any? ? Memo.open(files.first, version) : nil
end
def find_all(options) #nodoc
map do |record|
if record.match? options
@@ -196,11 +207,11 @@
def get_header_info #nodoc
@data.rewind
@version, @record_count, @header_length, @record_length, encoding_key =
@data.read(DBF_HEADER_SIZE).unpack("H2 x3 V v2 x17H2")
- @encoding = self.class.encodings[encoding_key] if "".respond_to? :encoding
+ @encoding = self.class.encodings[encoding_key] if supports_encoding?
end
def seek(offset) #nodoc
@data.seek @header_length + offset
end
@@ -212,10 +223,10 @@
def default_csv_path #nodoc
File.basename(@data.path, '.dbf') + '.csv'
end
def self.encodings
- @encodings ||= YAML.load File.read(File.expand_path("../encodings.yml", __FILE__))
+ @encodings ||= YAML.load_file File.expand_path("../encodings.yml", __FILE__)
end
end
end