Class: DBLeftovers::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/db_leftovers/index.rb

Overview

Just a struct to hold all the info for one index:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, column_names, opts = {}) ⇒ Index

Returns a new instance of Index.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/db_leftovers/index.rb', line 8

def initialize(table_name, column_names, opts={})
  opts = {
    :where => nil,
    :function => nil,
    :unique => false,
    :using => nil
  }.merge(opts)
  opts.keys.each do |k|
    raise "Unknown option: #{k}" unless [:where, :function, :unique, :using, :name].include?(k)
  end
  if column_names.is_a?(String) and opts[:function].nil?
    opts[:function] = column_names
    column_names = []
  end
  @table_name = table_name.to_s
  @column_names = [column_names].flatten.map{|x| x.to_s}
  @where_clause = opts[:where]
  @index_function = opts[:function]
  @using_clause = opts[:using]
  @unique = !!opts[:unique]
  @index_name = (opts[:name] || choose_name(@table_name, @column_names, @index_function)).to_s

  raise "Indexes need a table!" unless @table_name
  raise "Indexes need at least column or an expression!" unless (@column_names.any? or @index_function)
  raise "Can't have both columns and an expression!" if (@column_names.size > 0 and @index_function)
end

Instance Attribute Details

#column_namesObject

Returns the value of attribute column_names.



5
6
7
# File 'lib/db_leftovers/index.rb', line 5

def column_names
  @column_names
end

#index_functionObject

Returns the value of attribute index_function.



5
6
7
# File 'lib/db_leftovers/index.rb', line 5

def index_function
  @index_function
end

#index_nameObject

Returns the value of attribute index_name.



5
6
7
# File 'lib/db_leftovers/index.rb', line 5

def index_name
  @index_name
end

#table_nameObject

Returns the value of attribute table_name.



5
6
7
# File 'lib/db_leftovers/index.rb', line 5

def table_name
  @table_name
end

#uniqueObject

Returns the value of attribute unique.



5
6
7
# File 'lib/db_leftovers/index.rb', line 5

def unique
  @unique
end

#using_clauseObject

Returns the value of attribute using_clause.



5
6
7
# File 'lib/db_leftovers/index.rb', line 5

def using_clause
  @using_clause
end

#where_clauseObject

Returns the value of attribute where_clause.



5
6
7
# File 'lib/db_leftovers/index.rb', line 5

def where_clause
  @where_clause
end

Instance Method Details

#equals(other) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/db_leftovers/index.rb', line 39

def equals(other)
  other.table_name == table_name and
  other.column_names == column_names and
  other.index_name == index_name and
  other.where_clause == where_clause and
  other.index_function == index_function and
  other.using_clause == using_clause and
  other.unique == unique
end

#to_sObject



49
50
51
# File 'lib/db_leftovers/index.rb', line 49

def to_s
  "<#{@index_name}: #{@table_name}.[#{column_names.join(",")}] unique=#{@unique}, where=#{@where_clause}, function=#{@index_function}, using=#{@using_clause}>"
end

#unique?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/db_leftovers/index.rb', line 35

def unique?
  !!@unique
end