Class: HoboFields::IndexSpec
- Inherits:
-
Object
- Object
- HoboFields::IndexSpec
- Defined in:
- lib/hobo_fields/index_spec.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#name ⇒ Object
Returns the value of attribute name.
-
#table ⇒ Object
Returns the value of attribute table.
-
#unique ⇒ Object
Returns the value of attribute unique.
Class Method Summary collapse
-
.for_model(model, old_table_name = nil) ⇒ Object
extract IndexSpecs from an existing table.
Instance Method Summary collapse
- #==(v) ⇒ Object (also: #eql?)
- #default_name? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(model, fields, options = {}) ⇒ IndexSpec
constructor
A new instance of IndexSpec.
- #to_add_statement(new_table_name) ⇒ Object
Constructor Details
#initialize(model, fields, options = {}) ⇒ IndexSpec
Returns a new instance of IndexSpec.
5 6 7 8 9 10 11 |
# File 'lib/hobo_fields/index_spec.rb', line 5 def initialize(model, fields, ={}) @model = model self.table = .delete(:table_name) || model.table_name self.fields = Array.wrap(fields).*.to_s self.name = .delete(:name) || model.connection.index_name(self.table, :column => self.fields) self.unique = .delete(:unique) || false end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
13 14 15 |
# File 'lib/hobo_fields/index_spec.rb', line 13 def fields @fields end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/hobo_fields/index_spec.rb', line 13 def name @name end |
#table ⇒ Object
Returns the value of attribute table.
13 14 15 |
# File 'lib/hobo_fields/index_spec.rb', line 13 def table @table end |
#unique ⇒ Object
Returns the value of attribute unique.
13 14 15 |
# File 'lib/hobo_fields/index_spec.rb', line 13 def unique @unique end |
Class Method Details
.for_model(model, old_table_name = nil) ⇒ Object
extract IndexSpecs from an existing table
16 17 18 19 20 21 |
# File 'lib/hobo_fields/index_spec.rb', line 16 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 |
Instance Method Details
#==(v) ⇒ Object Also known as: eql?
38 39 40 |
# File 'lib/hobo_fields/index_spec.rb', line 38 def ==(v) v.hash == hash end |
#default_name? ⇒ Boolean
23 24 25 |
# File 'lib/hobo_fields/index_spec.rb', line 23 def default_name? name == @model.connection.index_name(table, :column => fields) end |
#hash ⇒ Object
34 35 36 |
# File 'lib/hobo_fields/index_spec.rb', line 34 def hash [table, fields, name, unique].hash end |
#to_add_statement(new_table_name) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/hobo_fields/index_spec.rb', line 27 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 |