Module: DBF::FileHandler

Defined in:
lib/dbf/file_handler.rb

Class Method Summary collapse

Class Method Details

.data_path(data) ⇒ Object



35
36
37
38
39
# File 'lib/dbf/file_handler.rb', line 35

def data_path(data)
  return data if data.is_a?(String)

  data.path if data.respond_to?(:path)
end

.memo_search_path(path) ⇒ Object



41
42
43
44
45
# File 'lib/dbf/file_handler.rb', line 41

def memo_search_path(path)
  dirname = File.dirname(path)
  basename = File.basename(path, '.*')
  "#{dirname}/#{basename}*.{fpt,FPT,dbt,DBT}"
end

.open_data(data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dbf/file_handler.rb', line 7

def open_data(data)
  case data
  when StringIO
    data
  when String
    File.open(data, 'rb')
  else
    raise ArgumentError, 'data must be a file path or an IO-like object responding to #read and #seek' unless data.respond_to?(:read) && data.respond_to?(:seek)

    # DBF is a binary format; a File opened in text mode would corrupt
    # reads on Windows.
    data.binmode if data.respond_to?(:binmode)
    data
  end
rescue Errno::ENOENT
  raise DBF::FileNotFoundError, "file not found: #{data}"
end

.open_memo(data, memo, memo_class, version) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/dbf/file_handler.rb', line 25

def open_memo(data, memo, memo_class, version)
  if memo
    meth = memo.is_a?(String) ? :open : :new
    memo_class.send(meth, memo, version)
  elsif (path = data_path(data))
    found = Dir.glob(memo_search_path(path)).first
    found && memo_class.open(found, version)
  end
end