class Barely::Connection

Attributes

tables[R]
visitor[RW]

Public Class Methods

new(visitor = nil) click to toggle source
# File lib/barely.rb, line 14
def initialize(visitor = nil)
  @tables = %w{ users photos developers products}
  @columns = {
    'users' => [
      Column.new('id', :integer),
      Column.new('name', :string),
      Column.new('bool', :boolean),
      Column.new('created_at', :date)
    ],
    'products' => [
      Column.new('id', :integer),
      Column.new('price', :decimal)
    ]
  }
  @columns_hash = {
    'users' => Hash[@columns['users'].map { |x| [x.name, x] }],
    'products' => Hash[@columns['products'].map { |x| [x.name, x] }]
  }
  @primary_keys = {
    'users' => 'id',
    'products' => 'id'
  }
  @visitor = visitor
end

Public Instance Methods

columns(name, message = nil) click to toggle source
# File lib/barely.rb, line 51
def columns name, message = nil
  @columns[name.to_s]
end
columns_hash(table_name) click to toggle source
# File lib/barely.rb, line 39
def columns_hash table_name
  @columns_hash[table_name]
end
primary_key(name) click to toggle source
# File lib/barely.rb, line 43
def primary_key name
  @primary_keys[name.to_s]
end
quote(thing, column = nil) click to toggle source
# File lib/barely.rb, line 67
def quote thing, column = nil
  if column && !thing.nil?
    case column.type
    when :integer
      thing = thing.to_i
    when :string
      thing = thing.to_s
    end
  end

  case thing
  when DateTime
    "'#{thing.strftime("%Y-%m-%d %H:%M:%S")}'"
  when Date
    "'#{thing.strftime("%Y-%m-%d")}'"
  when true
    "'t'"
  when false
    "'f'"
  when nil
    'NULL'
  when Numeric
    thing
  else
    "'#{thing.to_s.gsub("'", "\\\\'")}'"
  end
end
quote_column_name(name) click to toggle source
# File lib/barely.rb, line 59
def quote_column_name name
  "\"#{name.to_s}\""
end
quote_table_name(name) click to toggle source
# File lib/barely.rb, line 55
def quote_table_name name
  "\"#{name.to_s}\""
end
schema_cache() click to toggle source
# File lib/barely.rb, line 63
def schema_cache
  self
end
table_exists?(name) click to toggle source
# File lib/barely.rb, line 47
def table_exists? name
  @tables.include? name.to_s
end