Skip to content

Instantly share code, notes, and snippets.

View le0pard's full-sized avatar
🕷️
Your friendly neighborhood coder

Oleksii Vasyliev le0pard

🕷️
Your friendly neighborhood coder
View GitHub Profile
@le0pard
le0pard / backbone_sync_hack.coffee
Created September 10, 2011 17:35 — forked from jumski/backbone_sync_hack.coffee
BackboneJS sync hack to namespace params when saving/updating model
###
Usage:
* add model.name property that will be used as a namespace in the json request
* put this code before your Backbone app code
* use toJSON() as usual (so there is no namespacing in your templates)
* your model's data will be sent under model.name key when calling save()
###
# save reference to Backbone.sync
Backbone.oldSync = Backbone.sync
@le0pard
le0pard / controller.rb
Created August 15, 2011 12:41
Standart ActionController and ActionController::Metal comparison for api (rails 3.1rc5)
class V1Controller < ApplicationController
before_filter :check_some
def index
response_hash = {:a => [1,2,3], :status => "OK"}
respond_to do |format|
format.xml { render :xml => response_hash.to_xml(:root => "response", :dasherize => false, :skip_types => true) }
format.json { render :json => response_hash, :callback => params[:callback] }
end
@le0pard
le0pard / gist:1058405
Created July 1, 2011 12:03
Skypekit ruby Makefile
SHELL = /bin/sh
#### Start of system configuration section. ####
srcdir = ../../../../ext/skypekit4ruby
topdir = /home/leo/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1
hdrdir = /home/leo/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1
arch_hdrdir = /home/leo/.rvm/rubies/ruby-1.9.2-p180/include/ruby-1.9.1/$(arch)
VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
@le0pard
le0pard / gist:972713
Created May 14, 2011 22:38
Ускоряем LIKE
prefix_test=# create table tags (
prefix_test(# tag text primary key,
prefix_test(# name text not null,
prefix_test(# shortname text,
prefix_test(# status char default 'S',
prefix_test(#
prefix_test(# check( status in ('S', 'R') )
prefix_test(# );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tags_pkey" for table "tags"
CREATE TABLE
@le0pard
le0pard / gist:972335
Created May 14, 2011 15:45
Куайн, Запрос который выводит сам себя
select a || ' from (select ' || quote_literal(a) || b || ', ' || quote_literal(b) || '::text as b) as quine' from (select 'select a || '' from (select '' || quote_literal(a) || b || '', '' || quote_literal(b) || ''::text as b) as quine'''::text as a, '::text as a'::text as b) as quine;
@le0pard
le0pard / gist:910793
Created April 8, 2011 21:32
Luhn algorithm
CREATE OR REPLACE FUNCTION luhn_verify(int8) RETURNS BOOLEAN AS $$
-- Take the sum of the
-- doubled digits and the even-numbered undoubled digits, and see if
-- the sum is evenly divisible by zero.
SELECT
-- Doubled digits might in turn be two digits. In that case,
-- we must add each digit individually rather than adding the
-- doubled digit value to the sum. Ie if the original digit was
-- `6' the doubled result was `12' and we must add `1+2' to the
-- sum rather than `12'.
@le0pard
le0pard / gist:910763
Created April 8, 2011 21:22
Random Range
CREATE OR REPLACE FUNCTION random(numeric, numeric)
RETURNS numeric AS
$$
SELECT ($1 + ($2 - $1) * random())::numeric;
$$ LANGUAGE 'sql' VOLATILE;
@le0pard
le0pard / gist:910749
Created April 8, 2011 21:11
Return default value of field in table
CREATE OR REPLACE FUNCTION ret_def(text,text,text) RETURNS text AS $$
SELECT
COLUMNS.column_default::text
FROM
information_schema.COLUMNS
WHERE table_name = $2
AND table_schema = $1
AND column_name = $3
$$ LANGUAGE sql IMMUTABLE;
@le0pard
le0pard / gist:910728
Created April 8, 2011 20:57
Count estimate
CREATE LANGUAGE plpgsql;
CREATE FUNCTION count_estimate(query text) RETURNS integer AS $$
DECLARE
rec record;
rows integer;
BEGIN
FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
rows := substring(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
EXIT WHEN rows IS NOT NULL;
END LOOP;
@le0pard
le0pard / gist:910696
Created April 8, 2011 20:43
Finding the total size of your biggest tables
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size(C.oid) DESC
LIMIT 20;