Class: HoboFields::IndexSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/hobo_fields/index_spec.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  @model = model
  self.table = options.delete(:table_name) || model.table_name
  self.fields = Array.wrap(fields).*.to_s
  self.name = options.delete(:name) || model.connection.index_name(self.table, :column => self.fields)
  self.unique = options.delete(:unique) || false
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



13
14
15
# File 'lib/hobo_fields/index_spec.rb', line 13

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/hobo_fields/index_spec.rb', line 13

def name
  @name
end

#tableObject

Returns the value of attribute table.



13
14
15
# File 'lib/hobo_fields/index_spec.rb', line 13

def table
  @table
end

#uniqueObject

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

Returns:

  • (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

#hashObject



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