Class: DBLeftovers::Index
- Inherits:
-
Object
- Object
- DBLeftovers::Index
- Defined in:
- lib/db_leftovers/index.rb
Overview
Just a struct to hold all the info for one index:
Instance Attribute Summary collapse
-
#column_names ⇒ Object
Returns the value of attribute column_names.
-
#index_function ⇒ Object
Returns the value of attribute index_function.
-
#index_name ⇒ Object
Returns the value of attribute index_name.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
-
#unique ⇒ Object
Returns the value of attribute unique.
-
#using_clause ⇒ Object
Returns the value of attribute using_clause.
-
#where_clause ⇒ Object
Returns the value of attribute where_clause.
Instance Method Summary collapse
- #equals(other) ⇒ Object
-
#initialize(table_name, column_names, opts = {}) ⇒ Index
constructor
A new instance of Index.
- #to_s ⇒ Object
- #unique? ⇒ Boolean
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?(Array) and column_names[0].is_a?(String) and opts[:function].nil? opts[:function] = column_names[0] 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_names ⇒ Object
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_function ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
Returns the value of attribute table_name.
5 6 7 |
# File 'lib/db_leftovers/index.rb', line 5 def table_name @table_name end |
#unique ⇒ Object
Returns the value of attribute unique.
5 6 7 |
# File 'lib/db_leftovers/index.rb', line 5 def unique @unique end |
#using_clause ⇒ Object
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_clause ⇒ Object
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_s ⇒ Object
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
35 36 37 |
# File 'lib/db_leftovers/index.rb', line 35 def unique? !!@unique end |