Class: CleanSweep::TableSchema::IndexSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/clean_sweep/table_schema/index_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, model, unique = false) ⇒ IndexSchema

Returns a new instance of IndexSchema.



5
6
7
8
9
10
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 5

def initialize name, model, unique = false
  @model = model
  @columns = []
  @name = name
  @unique = unique
end

Instance Attribute Details

#ascendingObject

Returns the value of attribute ascending.



3
4
5
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 3

def ascending
  @ascending
end

#columnsObject

Returns the value of attribute columns.



3
4
5
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 3

def columns
  @columns
end

#dest_modelObject

Returns the value of attribute dest_model.



3
4
5
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 3

def dest_model
  @dest_model
end

#first_onlyObject

Returns the value of attribute first_only.



3
4
5
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 3

def first_only
  @first_only
end

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 3

def model
  @model
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 3

def name
  @name
end

Instance Method Details

#<<(col_name) ⇒ Object

Add a column



13
14
15
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 13

def << col_name
  @columns << CleanSweep::TableSchema::ColumnSchema.new(col_name, model)
end

#add_columns_to(columns) ⇒ Object

Take columns referenced by this index and add them to the list if they are not present. Record their position in the list because the position will be where they are located in a row of values passed in later to #scope_to_next_chunk



24
25
26
27
28
29
30
31
32
33
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 24

def add_columns_to columns
  @columns.each do | column |
    pos = columns.index column
    if pos.nil?
      columns << column
      pos = columns.size - 1
    end
    column.select_position = pos
  end
end

#order(scope) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 35

def order(scope)
  direction = ascending ? 'ASC' : 'DESC'
  if @first_only
    scope.order("#{columns.first.quoted_name} #{direction}")
  else
    scope.order(columns.map { |col| "#{col.quoted_name} #{direction}"}.join(","))
  end
end

#scope_to_next_chunk(scope, last_row) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 44

def scope_to_next_chunk(scope, last_row)
  query_args = {}
  if @first_only
    query_args[columns.first.name] = columns.first.value(last_row)
  else
    columns.each do |column|
      query_args[column.name] = column.value(last_row)
    end
  end
  scope.where(chunk_clause, query_args)
end

#unique?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/clean_sweep/table_schema/index_schema.rb', line 17

def unique?
  @unique
end