Class: Table

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

Overview

Deprecated. So very deprecated.

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, #faked_count, #field_names_for, #mdb_export, #mdb_schema, #mdb_sql, #mdb_tables, #mdb_truth, #mdb_version, #methodize, #sanitize!, #sql_select_where, #table_to_csv, #valid_file?

Constructor Details

#initialize(mdb, table_name, prefix) ⇒ Table

Returns a new instance of Table.



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

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.



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

def columns
  @columns
end

#mdb_fileObject (readonly)

Returns the value of attribute mdb_file.



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

def mdb_file
  @mdb_file
end

#primary_keyObject

Returns the value of attribute primary_key.



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

def primary_key
  @primary_key
end

#record_structObject (readonly)

Returns the value of attribute record_struct.



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

def record_struct
  @record_struct
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#[](method_name) ⇒ Object



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

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

#column_namesObject

returns an array of column names



27
28
29
# File 'lib/active_mdb/table.rb', line 27

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

#create_record_structObject



31
32
33
34
# File 'lib/active_mdb/table.rb', line 31

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

#find_all(conditions_hash = {}) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/active_mdb/table.rb', line 54

def find_all(conditions_hash={})
  if conditions_hash.empty?
    return sql_select_where(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

returns the first record that meets the equality (sometimes LIKE) conditions of the supplied hash. No comparison operators available at the moment.

find_first :superhero_name => ‘The Ironist’, :powers => ‘Wit’

mdb-sql doesn’t implement LIMIT yet, so this method pulls all results and calls Array#first on them. Ooky.



47
48
49
50
51
# File 'lib/active_mdb/table.rb', line 47

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

#to_csvObject



36
37
38
# File 'lib/active_mdb/table.rb', line 36

def to_csv
  table_to_csv(mdb_file, table_name)
end