Sha256: 386f5866d99da26e4696ec72fd5cd233aa33dbbbbe552a419a1bbc81e7c20e7e

Contents?: true

Size: 1.22 KB

Versions: 19

Compression:

Stored size: 1.22 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
        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

19 entries across 19 versions & 3 rubygems

Version Path
ihoka-friendly-0.8.0.pre lib/friendly/table_creator.rb
ihoka-friendly-0.7.1.2 lib/friendly/table_creator.rb
ihoka-friendly-0.7.1.1 lib/friendly/table_creator.rb
ihoka-friendly-0.7.1 lib/friendly/table_creator.rb
ihoka-friendly-0.7.0 lib/friendly/table_creator.rb
ihoka-friendly-0.6.3 lib/friendly/table_creator.rb
ihoka-friendly-0.6.2 lib/friendly/table_creator.rb
friendly-0.6.0 lib/friendly/table_creator.rb
honkster-friendly-0.5.3 lib/friendly/table_creator.rb
honkster-friendly-0.5.2 lib/friendly/table_creator.rb
honkster-friendly-0.5.1 lib/friendly/table_creator.rb
friendly-0.5.1 lib/friendly/table_creator.rb
friendly-0.5.0 lib/friendly/table_creator.rb
friendly-0.4.5 lib/friendly/table_creator.rb
friendly-0.4.4 lib/friendly/table_creator.rb
friendly-0.4.3 lib/friendly/table_creator.rb
friendly-0.4.2 lib/friendly/table_creator.rb
friendly-0.4.1 lib/friendly/table_creator.rb
friendly-0.4.0 lib/friendly/table_creator.rb