Class: DBF::Database::Foxpro

Inherits:
Object
  • Object
show all
Defined in:
lib/dbf/database/foxpro.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Foxpro

Opens a DBF::Database::Foxpro Examples:

 # working with a database stored on the filesystem
 db = DBF::Database::Foxpro.new 'path_to_db/database.dbc'

# Calling a table
contacts = db.contacts.record(0)


19
20
21
22
23
24
25
26
27
# File 'lib/dbf/database/foxpro.rb', line 19

def initialize(path)
  @path = path
  @dirname = File.dirname(@path)
  @db = DBF::Table.new(@path)
  @tables = extract_dbc_data

rescue Errno::ENOENT
  raise DBF::FileNotFoundError, "file not found: #{data}"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

nodoc



57
58
59
60
61
62
63
# File 'lib/dbf/database/foxpro.rb', line 57

def method_missing(method, *args) # nodoc
  if table_names.index(method.to_s)
    table method.to_s
  else
    super
  end
end

Instance Method Details

#table(name) ⇒ Object

Returns table with given name

Returns:

  • Table



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

def table(name)
  Table.new(table_path name) do |table|
    table.long_names = @tables[name]
  end
end

#table_namesObject



29
30
31
# File 'lib/dbf/database/foxpro.rb', line 29

def table_names
  @tables.keys
end

#table_path(name) ⇒ Object

Searches the database directory for the table’s dbf file and returns the absolute path. Ensures case-insensitivity on any platform.

Returns:

  • String



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dbf/database/foxpro.rb', line 45

def table_path(name)
  example = File.join(@dirname, "#{name}.dbf")
  glob = File.join(@dirname, '*')
  path = Dir.glob(glob).find { |match| match.downcase == example.downcase }

  unless path && File.exist?(path)
    raise DBF::FileNotFoundError, "related table not found: #{name}"
  end

  path
end