Class: DBF::Database::Foxpro
- Inherits:
-
Object
- Object
- DBF::Database::Foxpro
- Defined in:
- lib/dbf/database/foxpro.rb
Instance Method Summary collapse
-
#initialize(path) ⇒ Foxpro
constructor
Opens a DBF::Database::Foxpro Examples: # working with a database stored on the filesystem db = DBF::Database::Foxpro.new ‘path_to_db/database.dbc’.
-
#method_missing(method, *args) ⇒ Object
:nodoc:.
- #respond_to_missing?(method) ⇒ Boolean
-
#table(name) ⇒ DBF::Table
Returns table with given name.
- #table_names ⇒ Object
-
#table_path(name) ⇒ String
Searches the database directory for the table’s dbf file and returns the absolute path.
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)
23 24 25 26 27 28 29 30 |
# File 'lib/dbf/database/foxpro.rb', line 23 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:
60 61 62 |
# File 'lib/dbf/database/foxpro.rb', line 60 def method_missing(method, *args) # :nodoc: table_names.index(method.to_s) ? table(method.to_s) : super end |
Instance Method Details
#respond_to_missing?(method) ⇒ Boolean
64 65 66 |
# File 'lib/dbf/database/foxpro.rb', line 64 def respond_to_missing?(method, *) table_names.index(method.to_s) || super end |
#table(name) ⇒ DBF::Table
Returns table with given name
40 41 42 43 44 |
# File 'lib/dbf/database/foxpro.rb', line 40 def table(name) Table.new table_path(name) do |table| table.long_names = @tables[name] end end |
#table_names ⇒ Object
32 33 34 |
# File 'lib/dbf/database/foxpro.rb', line 32 def table_names @tables.keys end |
#table_path(name) ⇒ String
Searches the database directory for the table’s dbf file and returns the absolute path. Ensures case-insensitivity on any platform.
51 52 53 54 55 56 57 58 |
# File 'lib/dbf/database/foxpro.rb', line 51 def table_path(name) glob = File.join(@dirname, "#{name}.dbf") path = Dir.glob(glob, File::FNM_CASEFOLD).first raise DBF::FileNotFoundError, "related table not found: #{name}" unless path && File.exist?(path) path end |