lib/dbf/table.rb in dbf-1.6.6 vs lib/dbf/table.rb in dbf-1.6.7

- old
+ new

@@ -5,11 +5,11 @@ class Table include Enumerable DBF_HEADER_SIZE = 32 - VERSION_DESCRIPTIONS = { + VERSIONS = { "02" => "FoxBase", "03" => "dBase III without memo file", "04" => "dBase IV without memo file", "05" => "dBase V without memo file", "30" => "Visual FoxPro", @@ -20,13 +20,11 @@ "8e" => "dBase IV with SQL table", "f5" => "FoxPro with memo file", "fb" => "FoxPro without memo file" } - FOXPRO_VERSIONS = VERSION_DESCRIPTIONS.map do |version, description| - version if description =~ /FoxPro/ - end.compact + FOXPRO_VERSIONS = VERSIONS.select {|k,v| v =~ /FoxPro/} attr_reader :version # Internal dBase version number attr_reader :record_count # Total number of records attr_accessor :encoding # Source encoding (for ex. :cp1251) @@ -39,24 +37,24 @@ @data = File.open(path, 'rb') get_header_info @memo = open_memo(path) end + # @return [TrueClass, FalseClass] def has_memo_file? - @memo && memo_file_format + !!@memo end - - def memo_file_format - @memo.format if has_memo_file? - end # Closes the table and memo file + # + # @return [TrueClass, FalseClass] def close @memo && @memo.close - @data.close + @data.close && @data.closed? end + # @return String def filename File.basename @data.path end # Calls block once for each record in the table. The record may be nil @@ -73,20 +71,22 @@ # # @param [Fixnum] index # @return [DBF::Record, NilClass] def record(index) seek(index * @record_length) - deleted_record? ? nil : DBF::Record.new(@data.read(@record_length), columns, version, @memo) + if !deleted_record? + DBF::Record.new(@data.read(@record_length), columns, version, @memo) + end end alias_method :row, :record # Human readable version description # # @return [String] def version_description - VERSION_DESCRIPTIONS[version] + VERSIONS[version] end # Generate an ActiveRecord::Schema # # xBase data types are converted to generic types as follows: @@ -185,20 +185,26 @@ "".respond_to? :encoding end private - def column_class - @column_class ||= FOXPRO_VERSIONS.include?(version) ? FoxproColumn : Column + def column_class #nodoc + @column_class ||= if FOXPRO_VERSIONS.keys.include?(version) + FoxproColumn + else + Column + end end - def column_count + def column_count #nodoc @column_count ||= (@header_length - DBF_HEADER_SIZE + 1) / DBF_HEADER_SIZE end def open_memo(path) #nodoc - files = Dir.glob("#{File.dirname(path)}/#{File.basename(path, '.*')}*.{fpt,FPT,dbt,DBT}") + 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 end def find_all(options) #nodoc map do |record| @@ -234,10 +240,10 @@ def default_csv_path #nodoc File.basename(@data.path, '.dbf') + '.csv' end - def self.encodings + def self.encodings #nodoc @encodings ||= YAML.load_file File.expand_path("../encodings.yml", __FILE__) end end end