Sha256: 1985bfe33edb929e55ec22a39b590b09fa156d45a43dd102282178dc3c5b2846

Contents?: true

Size: 1.24 KB

Versions: 16

Compression:

Stored size: 1.24 KB

Contents

module HoboFields

  class IndexSpec

    def initialize(model, fields, options={})
      @model = model
      self.table = options.delete(:table_name) || model.table_name
      self.fields = Array.wrap(fields).*.to_s
      self.name = options.delete(:name) || model.connection.index_name(self.table, :column => self.fields)
      self.unique = options.delete(:unique) || false
    end

    attr_accessor :table, :fields, :name, :unique

    # extract IndexSpecs from an existing table
    def self.for_model(model, old_table_name=nil)
      t = old_table_name || model.table_name
      model.connection.indexes(t).map do |i|
        self.new(model, i.columns, :name => i.name, :unique => i.unique, :table_name => old_table_name) unless model.ignore_indexes.include?(i.name)
      end.compact
    end

    def default_name?
      name == @model.connection.index_name(table, :column => fields)
    end

    def to_add_statement(new_table_name)
      r = "add_index :#{new_table_name}, #{fields.*.to_sym.inspect}"
      r += ", :unique => true" if unique
      r += ", :name => '#{name}'" unless default_name?
      r
    end

    def hash
      [table, fields, name, unique].hash
    end

    def ==(v)
      v.hash == hash
    end
    alias_method :eql?, :==

  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
hobofields-1.1.0 lib/hobo_fields/index_spec.rb
hobofields-1.0.3 lib/hobo_fields/index_spec.rb
hobofields-1.1.0.pre4 lib/hobo_fields/index_spec.rb
hobofields-1.1.0.pre3 lib/hobo_fields/index_spec.rb
hobofields-1.1.0.pre2 lib/hobo_fields/index_spec.rb
hobofields-1.1.0.pre0 lib/hobo_fields/index_spec.rb
hobofields-1.0.2 lib/hobo_fields/index_spec.rb
hobofields-1.0.1 lib/hobo_fields/index_spec.rb
hobofields-1.0.0 lib/hobo_fields/index_spec.rb
hobofields-0.9.106 lib/hobo_fields/index_spec.rb
hobofields-0.9.105 lib/hobo_fields/index_spec.rb
hobofields-0.9.104 lib/hobo_fields/index_spec.rb
hobofields-0.9.103 lib/hobo_fields/index_spec.rb
hobofields-0.9.102 lib/hobo_fields/index_spec.rb
hobofields-0.9.101 lib/hobo_fields/index_spec.rb
hobofields-0.9.100 lib/hobo_fields/index_spec.rb