Sha256: f9c3ab54385f5177fe55db47f6008da55bf3ef384120f00ab295cbd304514465

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 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
          column      :id, :bytea
          String      :attributes, :text => true
          Time        :created_at
          Time        :updated_at
        end
      end

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

        db.create_table(table.table_name) do
          column      :id, :bytea
          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 & 1 rubygems

Version Path
friendly_postgres-0.5.1 lib/friendly/table_creator.rb
friendly_postgres-0.4.5 lib/friendly/table_creator.rb
friendly_postgres-0.4.3 lib/friendly/table_creator.rb