Skip to content

Instantly share code, notes, and snippets.

@evtuhovich
Created December 18, 2012 11:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evtuhovich/4327395 to your computer and use it in GitHub Desktop.
Save evtuhovich/4327395 to your computer and use it in GitHub Desktop.
Код к докладу "Нетрадиционное использование Ruby и PostgreSQL" http://www.slideshare.net/evtuhovich/ruby-postgresql
require 'rubygems'
require 'pg'
require 'yaml'
class DB
def initialize(debug = true)
@conn = PG::Connection.open()
@debug = debug
end
def things
@things ||= Collection.new('things', self)
end
def conn
@conn
end
def exec(query)
puts query if @debug
if @timing
start = Time.now
end
res = @conn.exec query
if @timing
puts Time.now - start
end
res
end
def timing=(value)
@timing = value
end
def debug=(value)
@debug = value
end
end
class Collection
def initialize(name, db)
@name = name
@db = db
end
def insert(obj)
serialize = obj.to_yaml
@db.exec("INSERT INTO #{@name}(value) VALUES('#{@db.conn.escape_string(serialize)}')")
end
def count(conditions = {})
query = "SELECT count(*) FROM #{@name}"
cond = []
conditions.each do | k, v|
cond << "rmongorb_get_key('#{k}'::text, value) = '#{v}'"
end
unless cond.empty?
query += ' WHERE ' + cond.join(' AND ')
end
@db.exec(query)[0]["count"].to_i
end
def find(conditions = {}, options = {})
query = "SELECT * FROM #{@name}"
cond = []
conditions.each do | k, v|
cond << "rmongorb_get_key('#{k}'::text, value) = '#{v}'"
end
unless cond.empty?
query += ' WHERE ' + cond.join(' AND ')
end
if limit = options[:limit] || 30
query += " LIMIT #{limit}"
end
if offset = options[:offet]
query += " OFFSET #{offset}"
end
res = @db.exec(query)
res.inject([]) do |result, tuple|
value = YAML.load(tuple['value'])
value['id'] = tuple['id']
result << value
result
end
end
def ensureIndex(keys)
keys.each do |k, v|
query = "CREATE INDEX ON #{@name} (rmongorb_get_key('#{k}'::text, value))"
@db.exec query
end
end
end
def db()
@db ||= DB.new
@db
end
=begin
CREATE OR REPLACE FUNCTION rmongorb_get_key(key text, value text) RETURNS text AS '
require "yaml"
obj = YAML.load(value)
obj[key]
' LANGUAGE 'plruby' IMMUTABLE;
=end
function! Serpinski()
ruby << EOF
def draw(arr, x, y)
arr[x][y] = '*'
end
def carpet(arr, size, x, y)
if size == 1
draw(arr, x, y)
else
new_size = size / 3
carpet(arr, new_size, x, y)
carpet(arr, new_size, x + new_size, y)
carpet(arr, new_size, x + new_size * 2, y)
carpet(arr, new_size, x, y + new_size)
carpet(arr, new_size, x + new_size * 2, y + new_size)
carpet(arr, new_size, x, y + new_size * 2)
carpet(arr, new_size, x + new_size, y + new_size * 2)
carpet(arr, new_size, x + new_size * 2, y + new_size * 2)
end
end
arr = Array.new(27) { Array.new }
carpet(arr, 27, 0, 0)
arr.each_with_index do |line, i|
l = line.map { |e| e.nil? ? ' ' : e }
Vim::Buffer.current.append(Vim::Buffer.current.line_number + i, l.join(''))
end
EOF
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment