Class: Table

Inherits:
Object
  • Object
show all
Includes:
MDBTools
Defined in:
lib/active_mdb/table.rb

Overview

require ‘mdb_tools’

Constant Summary

Constants included from MDBTools

MDBTools::BACKENDS, MDBTools::DELIMITER, MDBTools::LINEBREAK, MDBTools::SANITIZER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MDBTools

#arrays_to_hashes, #backends, #check_file, #check_table, #compile_conditions, #delimited_to_arrays, #describe_table, #mdb_export, #mdb_schema, #mdb_sql, #mdb_tables, #mdb_truth, #methodize, #sanitize!, #sql_select, #table_to_csv

Constructor Details

#initialize(mdb, table_name, prefix) ⇒ Table

Returns a new instance of Table.



10
11
12
13
14
15
16
17
18
# File 'lib/active_mdb/table.rb', line 10

def initialize(mdb, table_name, prefix)
  @mdb_file = check_file(mdb.mdb_file)
  @table_name = check_table(@mdb_file, table_name)
  # @schema = mdb_schema(@mdb_file, @table_name)
  @columns = describe_table(mdb_file, table_name).map do |column|
    Column.new_from_describe(column)
  end
  @record_struct = create_record_struct
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



6
7
8
# File 'lib/active_mdb/table.rb', line 6

def columns
  @columns
end

#mdb_fileObject (readonly)

Returns the value of attribute mdb_file.



6
7
8
# File 'lib/active_mdb/table.rb', line 6

def mdb_file
  @mdb_file
end

#primary_keyObject

Returns the value of attribute primary_key.



7
8
9
# File 'lib/active_mdb/table.rb', line 7

def primary_key
  @primary_key
end

#record_structObject (readonly)

Returns the value of attribute record_struct.



6
7
8
# File 'lib/active_mdb/table.rb', line 6

def record_struct
  @record_struct
end

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/active_mdb/table.rb', line 6

def schema
  @schema
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



6
7
8
# File 'lib/active_mdb/table.rb', line 6

def table_name
  @table_name
end

Instance Method Details

#[](method_name) ⇒ Object



21
22
23
# File 'lib/active_mdb/table.rb', line 21

def [](method_name)
  self.columns.detect {|c| c.method_name == method_name }
end

#column_namesObject



25
26
27
# File 'lib/active_mdb/table.rb', line 25

def column_names
  columns.collect {|x| methodize(x.method_name).to_sym}
end

#create_record_structObject



29
30
31
32
# File 'lib/active_mdb/table.rb', line 29

def create_record_struct
  attributes = columns.collect {|column| column.method_name.to_sym}
  Struct.new( *attributes)
end

#find_all(conditions_hash = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/active_mdb/table.rb', line 48

def find_all(conditions_hash={})
  if conditions_hash.empty?
    return sql_select(mdb_file, table_name, nil, '1 = 1').collect {|r| create_record(r)}
  end
  rekey_hash(conditions_hash)
  sql_search(conditions_hash).collect {|r| create_record(r) }
end

#find_first(conditions_hash) ⇒ Object



42
43
44
45
46
# File 'lib/active_mdb/table.rb', line 42

def find_first(conditions_hash)
  rekey_hash(conditions_hash)
  result = sql_search(conditions_hash).first
  create_record(result) 
end

#to_csvObject



34
35
36
# File 'lib/active_mdb/table.rb', line 34

def to_csv
  table_to_csv(mdb_file, table_name)
end

#to_sqlObject



38
39
40
# File 'lib/active_mdb/table.rb', line 38

def to_sql
  raise 'not implemented'
end