class MigrationValidators::Spec::Support::ColumnWrapper

Attributes

column_name[RW]
last_exception[RW]

Public Class Methods

new(column_name, table_wrapper, db) click to toggle source
# 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
to_array(values) click to toggle source
# 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

Private Class Methods

quote(value) click to toggle source
# File lib/migration_validators/spec/support/column_wrapper.rb, line 49
def self.quote value
  value = "'#{value}" unless value.starts_with?("'")
  value = "#{value}'" unless value.ends_with?("'")
  value
end

Public Instance Methods

drop() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

execute(value, statement) click to toggle source
# File lib/migration_validators/spec/support/column_wrapper.rb, line 56
def execute value, statement
  @last_exception = nil

  begin
    @db.execute(statement) 
  rescue Exception => e 
    @last_exception = e
  end
end