Class: Miguel::Schema::Index

Inherits:
Object
  • Object
show all
Includes:
Output
Defined in:
lib/miguel/schema.rb

Overview

Class representing database index.

Constant Summary collapse

IGNORED_OPTS =

Options we ignore when comparing.

[ :null ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

#out_canonic_opts, #out_columns, #out_default, #out_default_opts, #out_name, #out_opts, #out_table_name, #out_type

Constructor Details

#initialize(table, columns, opts = {}) ⇒ Index

Create new index for given column(s).



214
215
216
217
218
# File 'lib/miguel/schema.rb', line 214

def initialize( table, columns, opts = {} )
  @table = table
  @columns = [ *columns ]
  @opts = opts
end

Instance Attribute Details

#columnsObject (readonly)

Index column(s) and options.



211
212
213
# File 'lib/miguel/schema.rb', line 211

def columns
  @columns
end

#optsObject (readonly)

Index column(s) and options.



211
212
213
# File 'lib/miguel/schema.rb', line 211

def opts
  @opts
end

#tableObject (readonly)

The table this index belongs to.



208
209
210
# File 'lib/miguel/schema.rb', line 208

def table
  @table
end

Instance Method Details

#==(other) ⇒ Object

Compare one index with another one.



236
237
238
239
240
# File 'lib/miguel/schema.rb', line 236

def == other
  other.is_a?( Index ) &&
  columns == other.columns &&
  canonic_opts == other.canonic_opts
end

#canonic_optsObject

Get the index options, in a canonic way.



224
225
226
227
228
# File 'lib/miguel/schema.rb', line 224

def canonic_opts
  o = { :unique => false, :name => default_index_name }
  o.merge!( opts )
  o.delete_if{ |key, value| IGNORED_OPTS.include? key }
end

#default_index_nameObject

Get default index name for this index.



231
232
233
# File 'lib/miguel/schema.rb', line 231

def default_index_name
  [ table.name, *columns, :index ].join( '_' ).to_sym
end

#dump(out) ⇒ Object

Dump index definition.



243
244
245
# File 'lib/miguel/schema.rb', line 243

def dump( out )
  out << "index #{out_columns}#{out_opts}"
end