Class: RStore::BaseTable

Inherits:
Object
  • Object
show all
Defined in:
lib/rstore/base_table.rb

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Hash{Symbol=>BaseTable}) table_classes (readonly)

A Hash holding subclasses of RStore::BaseTable

Examples:

class ProductsTable < RStore::BaseTable
  create #...
end

class DataTable < RStore::BaseTable
  create #...
end

RStore::BaseTable.table_classes
#=> {:products=>ProductsTable, :data=>DataTable} 


21
22
23
# File 'lib/rstore/base_table.rb', line 21

def table_classes
  @table_classes
end

Class Method Details

+ create { ... }

Note:

To be called when defining of a subclass of RStore::BaseTable

Note:

You need to define an extra column for an auto-incrementing primary key.

This method returns an undefined value.

Define the table layout.

Examples:

class ProductsTable < RStore::BaseTable

  create do
    primary_key :id, :allow_null => false
    String      :product
    Integer     :quantity
    Float       :price
    Date        :created_at
    DateTime    :min_demand
    Time        :max_demand
    Boolean     :on_stock, :allow_null => false, :default => false
  end
end

Yields:



50
51
52
53
54
55
56
# File 'lib/rstore/base_table.rb', line 50

def self.create &block
  class << self
    attr_reader :table_info
  end

  @table_info = block
end

+ (Symbol) name

Returns The lower-case class name without the Table postfix

Examples:

class ProductsTable < RStore::BaseTable

  create do
    primary_key :id, :allow_null => false
    String      :product
    Integer     :quantity
    Float       :price
    Date        :created_at
    DateTime    :min_demand
    Time        :max_demand
    Boolean     :on_stock, :allow_null => false, :default => false
  end
end

ProductsTable.name
#=> :products


87
88
89
# File 'lib/rstore/base_table.rb', line 87

def self.name  
  super.gsub!(/Table/,'').downcase.to_sym
end