Class: HoboFields::Model::IndexSpec
- Inherits:
-
Object
- Object
- HoboFields::Model::IndexSpec
- Defined in:
- lib/hobo_fields/model/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.
-
#where ⇒ Object
Returns the value of attribute where.
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.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/hobo_fields/model/index_spec.rb', line 6 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 if [:where] self.where = "#{.delete(:where)}" self.where = "(#{self.where})" unless self.where.start_with?('(') end end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
18 19 20 |
# File 'lib/hobo_fields/model/index_spec.rb', line 18 def fields @fields end |
#name ⇒ Object
Returns the value of attribute name.
18 19 20 |
# File 'lib/hobo_fields/model/index_spec.rb', line 18 def name @name end |
#table ⇒ Object
Returns the value of attribute table.
18 19 20 |
# File 'lib/hobo_fields/model/index_spec.rb', line 18 def table @table end |
#unique ⇒ Object
Returns the value of attribute unique.
18 19 20 |
# File 'lib/hobo_fields/model/index_spec.rb', line 18 def unique @unique end |
#where ⇒ Object
Returns the value of attribute where.
18 19 20 |
# File 'lib/hobo_fields/model/index_spec.rb', line 18 def where @where end |
Class Method Details
.for_model(model, old_table_name = nil) ⇒ Object
extract IndexSpecs from an existing table
21 22 23 24 25 26 |
# File 'lib/hobo_fields/model/index_spec.rb', line 21 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, :where => i.where, :table_name => old_table_name) unless model.ignore_indexes.include?(i.name) end.compact end |
Instance Method Details
#==(v) ⇒ Object Also known as: eql?
54 55 56 |
# File 'lib/hobo_fields/model/index_spec.rb', line 54 def ==(v) v.hash == hash end |
#default_name? ⇒ Boolean
28 29 30 |
# File 'lib/hobo_fields/model/index_spec.rb', line 28 def default_name? name == @model.connection.index_name(table, :column => fields) end |
#hash ⇒ Object
50 51 52 |
# File 'lib/hobo_fields/model/index_spec.rb', line 50 def hash [table, fields, name, unique, where].hash end |
#to_add_statement(new_table_name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hobo_fields/model/index_spec.rb', line 32 def to_add_statement(new_table_name) r = "add_index :#{new_table_name}, #{fields.*.to_sym.inspect}" r += ", :unique => true" if unique r += ", :where => '#{self.where}'" if self.where.present? if default_name? check_name = @model.connection.index_name(self.table, :column => self.fields) else check_name = name end if check_name.length > @model.connection.index_name_length r += ", :name => '#{name[0,@model.connection.index_name_length]}'" $stderr.puts("WARNING: index name #{check_name} too long, trimming") else r += ", :name => '#{name}'" unless default_name? end r end |