Sha256: 768f9eb4c5552e3edebc26b3fcd7afa58406f3ec68ebb3f95bc4d4f8db0d0437

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    class EnsuredTable < Table

      def column(column_name, type, options = {})
        if column_exists?(column_name)
          unless column_exists?(column_name, type, options)
            change(column_name, type, options)
          end
        else
          @base.add_column(@table_name, column_name, type, options)
        end
      end

      def remove(column_name)
        super if column_exists?(column_name)
      end

      %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
        class_eval <<-EOV
          def #{column_type}(*args)                   # def string(*args)
            define_columns('#{column_type}', *args)    #   define_column('string', *args)
          end                                         # end
        EOV
      end

      def define_columns(column_type, *args)
        options = args.extract_options!
        column_names = args

        column_names.each do |name|
          column_def = build_column_definition(name, column_type, options)
          def_options = column_def.members.inject({}){|h, k| h[k.to_sym] = column_def[k]; h;}
          #debugger
          column(name, column_type.to_sym, def_options)
        end
      end

      def build_column_definition(column_name, column_type, options = {})
        column = ColumnDefinition.new(@base, column_name, column_type)
        if options[:limit]
          column.limit = options[:limit]
        elsif native[column_type.to_sym].is_a?(Hash)
          column.limit = native[column_type.to_sym][:limit]
        end
        column.precision = options[:precision]
        column.scale = options[:scale]
        column.default = options[:default]
        column.null = options[:null]
        column
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ensured_schema-0.1.3 lib/ensured_schema/ensured_table.rb
ensured_schema-0.1.2 lib/ensured_schema/ensured_table.rb