Class: MigrationValidators::Spec::Support::ColumnWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/migration_validators/spec/support/column_wrapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_name, table_wrapper, db) ⇒ ColumnWrapper

Returns a new instance of ColumnWrapper.



10
11
12
13
14
# File 'lib/migration_validators/spec/support/column_wrapper.rb', line 10

def initialize column_name, table_wrapper, db
  @column_name = column_name
  @table_wrapper = table_wrapper
  @db = db
end

Instance Attribute Details

#column_nameObject

Returns the value of attribute column_name.



7
8
9
# File 'lib/migration_validators/spec/support/column_wrapper.rb', line 7

def column_name
  @column_name
end

#last_exceptionObject

Returns the value of attribute last_exception.



8
9
10
# File 'lib/migration_validators/spec/support/column_wrapper.rb', line 8

def last_exception
  @last_exception
end

Class Method Details

.to_array(values) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/migration_validators/spec/support/column_wrapper.rb', line 32

def self.to_array values
  values.collect do |value|
    case value.class.name
      when "Range" then to_array(value.to_a)
      when "Array" then to_array(value)
      when "String" then ['NULL', "''"].include?(value.upcase) ? value : quote(value)
      when "Date" then quote(value.strftime('%Y-%m-%d'))
      when "Time" then quote(value.strftime('%Y-%m-%d %H:%M:%S'))
      when "DateTime" then quote(value.strftime('%Y-%m-%d %H:%M:%S'))
      when "NilClass" then 'NULL'
      else value
    end
  end.flatten
end

Instance Method Details

#dropObject



16
17
18
# File 'lib/migration_validators/spec/support/column_wrapper.rb', line 16

def drop 
  @db.remove_column @table_wrapper.table_name, column_name
end

#insert(*values) ⇒ Object



26
27
28
29
30
# File 'lib/migration_validators/spec/support/column_wrapper.rb', line 26

def insert *values
  @last_exception = nil
  ColumnWrapper.to_array(values).each {|value| execute(value, "INSERT INTO #{@table_wrapper.table_name}(#{column_name}) VALUES(#{value})")}
  self
end

#update(*values) ⇒ Object



20
21
22
23
24
# File 'lib/migration_validators/spec/support/column_wrapper.rb', line 20

def update *values
  @last_exception = nil
  ColumnWrapper.to_array(values).each{|value| execute(value, "UPDATE #{@table_wrapper.table_name} SET #{column_name} = #{value}")}
  self
end