lib/dbf/table.rb in dbf-3.1.3 vs lib/dbf/table.rb in dbf-4.0.0
- old
+ new
@@ -3,10 +3,11 @@
end
# DBF::Table is the primary interface to a single DBF file and provides
# methods for enumerating and searching the records.
class Table
+ extend Forwardable
include Enumerable
include ::DBF::Schema
DBF_HEADER_SIZE = 32
@@ -38,10 +39,15 @@
}.freeze
attr_accessor :encoding
attr_writer :name
+ def_delegator :header, :header_length
+ def_delegator :header, :record_count
+ def_delegator :header, :record_length
+ def_delegator :header, :version
+
# Opens a DBF::Table
# Examples:
# # working with a file stored on the filesystem
# table = DBF::Table.new 'data.dbf'
#
@@ -102,11 +108,11 @@
# 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]
def each
- header.record_count.times { |i| yield record(i) }
+ record_count.times { |i| yield record(i) }
end
# @return [String]
def filename
File.basename(@data.path) if @data.respond_to?(:path)
@@ -167,22 +173,16 @@
# @param [Integer] index
# @return [DBF::Record, NilClass]
def record(index)
seek_to_record(index)
return nil if deleted_record?
- DBF::Record.new(@data.read(header.record_length), columns, version, @memo)
+
+ DBF::Record.new(@data.read(record_length), columns, version, @memo)
end
alias row record
- # Total number of records
- #
- # @return [Integer]
- def record_count
- @record_count ||= header.record_count
- 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 STDOUT
def to_csv(path = nil)
@@ -190,17 +190,10 @@
csv = CSV.new(out_io, force_quotes: true)
csv << column_names
each { |record| csv << record.to_a }
end
- # Internal dBase version number
- #
- # @return [String]
- def version
- @version ||= header.version
- end
-
# Human readable version description
#
# @return [String]
def version_description
VERSIONS[version]
@@ -220,31 +213,32 @@
end
end
def deleted_record? # :nodoc:
flag = @data.read(1)
- flag ? flag.unpack('a') == ['*'] : true
+ flag ? flag.unpack1('a') == '*' : true
end
def end_of_record? # :nodoc:
safe_seek { @data.read(1).ord == 13 }
end
def find_all(options) # :nodoc:
select do |record|
next unless record && record.match?(options)
+
yield record if block_given?
record
end
end
def find_first(options) # :nodoc:
detect { |record| record && record.match?(options) }
end
def foxpro? # :nodoc:
- FOXPRO_VERSIONS.keys.include? version
+ FOXPRO_VERSIONS.key?(version)
end
def header # :nodoc:
@header ||= safe_seek do
@data.seek(0)
@@ -288,13 +282,13 @@
original_pos = @data.pos
yield.tap { @data.seek(original_pos) }
end
def seek(offset) # :nodoc:
- @data.seek(header.header_length + offset)
+ @data.seek(header_length + offset)
end
def seek_to_record(index) # :nodoc:
- seek(index * header.record_length)
+ seek(index * record_length)
end
end
end