Class: HoboFields::Model::IndexSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/hobo_fields/model/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.



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, 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
  if options[:where]
    self.where = "#{options.delete(:where)}"
    self.where = "(#{self.where})" unless self.where.start_with?('(')
  end
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



18
19
20
# File 'lib/hobo_fields/model/index_spec.rb', line 18

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/hobo_fields/model/index_spec.rb', line 18

def name
  @name
end

#tableObject

Returns the value of attribute table.



18
19
20
# File 'lib/hobo_fields/model/index_spec.rb', line 18

def table
  @table
end

#uniqueObject

Returns the value of attribute unique.



18
19
20
# File 'lib/hobo_fields/model/index_spec.rb', line 18

def unique
  @unique
end

#whereObject

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

Returns:

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

#hashObject



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