class Tachyon

Constants

VERSION

Public Class Methods

connection_for(klass) click to toggle source
# File lib/tachyon.rb, line 14
def connection_for(klass)
  return @@connection_cache[klass] if @@connection_cache.has_key?(klass)
  @@connection_cache[klass] = klass.connection
end
dump(record) click to toggle source
# File lib/tachyon.rb, line 38
def dump(record)
  record.attributes_before_type_cast.map do |key, value|
    [key.to_sym, dump_attribute(value)]
  end.to_h
end
dump_attribute(attribute) click to toggle source
# File lib/tachyon.rb, line 44
def dump_attribute(attribute)
  case attribute
  when Time then attribute.to_s(:db)
  when Date then attribute.to_s(:db)
  when TrueClass then 1
  when FalseClass then 0
  else attribute
  end
end
insert(klass, data) click to toggle source
# File lib/tachyon.rb, line 8
def insert(klass, data)
  connection_for(klass).execute(sql_for(klass, data))
rescue ActiveRecord::RecordNotUnique
  # NO OP
end
quote_data(data) click to toggle source
# File lib/tachyon.rb, line 26
def quote_data(data)
  data.map {|value| quote_value(value) }
end
quote_value(value) click to toggle source
# File lib/tachyon.rb, line 30
def quote_value(value)
  case value
  when String then "'#{value.gsub("'", "''")}'"
  when NilClass then "NULL"
  else value
  end
end
sql_for(klass, data) click to toggle source
# File lib/tachyon.rb, line 19
def sql_for(klass, data)
  columns = "`" + data.keys.join("`, `") + "`"
  values = quote_data(data.values).join(", ")

  "INSERT INTO `#{klass.table_name}` (#{columns}) VALUES (#{values})"
end