Sha256: 84468ee7afc0f26fc54f5e208bb577802cbce914db9ee06591519453384a85be

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Friendly
  class TableCreator
    attr_reader :db, :attr_klass

    def initialize(db = Friendly.db, attr_klass = Friendly::Attribute)
      @db         = db
      @attr_klass = attr_klass
    end

    def create(table)
      unless db.table_exists?(table.table_name)
        case table
        when DocumentTable
          create_document_table(table) 
        when Index
          create_index_table(table)
        end
      end
    end

    protected
      def create_document_table(table)
        db.create_table(table.table_name) do
          primary_key :added_id
          binary      :id,         :size => 16
          String      :attributes, :text => true
          Time        :created_at
          Time        :updated_at
          
          unique      :id
        end
      end

      def create_index_table(table)
        attr = attr_klass # close around this please

        db.create_table(table.table_name) do
          binary :id, :size => 16
          table.fields.flatten.each do |f|    
            klass = table.klass.attributes[f].type
            type  = attr.custom_type?(klass) ? attr.sql_type(klass) : klass
            column(f, type)
          end
          primary_key table.fields.flatten + [:id]
          unique :id
        end
      end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
wayne-friendly-0.5.1 lib/friendly/table_creator.rb
wego-friendly-0.5.1 lib/friendly/table_creator.rb
arunthampi-friendly-0.5.1 lib/friendly/table_creator.rb