Class: RailsDb::Table

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/rails_db/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#column_names, #column_properties, #columns, #connection, #to_param

Constructor Details

#initialize(table_name) ⇒ Table

Returns a new instance of Table.



17
18
19
20
21
# File 'lib/rails_db/table.rb', line 17

def initialize(table_name)
  throw 'Access Denied' unless RailsDb::Database.accessible_tables.include?(table_name)
  @name = table_name
  @data = RailsDb::TableData.new(self)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#delete(id) ⇒ Object



44
45
46
# File 'lib/rails_db/table.rb', line 44

def delete(id)
  RailsDb::Database.delete(name, primary_key, id)
end

#indexesObject



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

def indexes
  RailsDb::Database.indexes(name)
end

#primary_keyObject



40
41
42
# File 'lib/rails_db/table.rb', line 40

def primary_key
  RailsDb::Database.primary_key(name)
end

#to_csvObject



23
24
25
26
27
28
29
30
# File 'lib/rails_db/table.rb', line 23

def to_csv
  CSV.generate do |csv|
    csv << column_names
    data.data.rows.each do |row|
      csv << row
    end
  end
end

#truncateObject



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

def truncate
  RailsDb::Database.truncate(name)
end