Skip to content

Instantly share code, notes, and snippets.

View fl00r's full-sized avatar

Peter Yanovich fl00r

View GitHub Profile
module ActiveRecord
module QueryMethods
alias :old_where :where
def where(opts, *rest)
opts.merge!({:locale => :ru})
old_where(opts, *rest)
end
end
end
@fl00r
fl00r / server_balancing.rb
Created April 12, 2012 09:06
Server balancing by file ID
require 'digest/sha1'
#
# servers -> [3, 5, 8]
# история количеств серверов
#
def fetch_server_no(acc, servers)
while server = servers.pop do
last = servers.last.to_i
# Для более равномерного размазывания по серверам
rest = Digest::SHA1.hexdigest("#{acc}_#{server}").to_i(16) % server
# Govnokod
def update
if params[:user][:password].blank?
params[:user].delete :password
params[:user].delete :password_confirmation
end
if @user.update_attributes(params[:user])
flash[:success] = "Edit Successful."
# config.ru
module Server
def self.call(env)
body = File.read("index.html")
[200, { "Content-Type" => "text/html", "Content-Length" => body.bytesize.to_s }, [body]]
end
end
run Server
@fl00r
fl00r / gist:4203478
Last active October 13, 2015 13:37
Truncating CouchBase. Facepalm
module Helpers
module Truncate
def teardown
teardown_couch
end
def teardown_couch
helpers = connection.design_docs["helpers"]
unless helpers && helpers.views.include?("truncate")
create_view
# Ruby
module App
extend self
def call(env)
[
200,
{ 'Content-Type' => 'application/json' },
['{"message": "Hello World"}']
]
@fl00r
fl00r / gist:5427088
Last active December 16, 2015 11:28
class Foo
def Foo.bar
end
class << Foo
def baz
end
end
end
@fl00r
fl00r / gist:5955527
Created July 9, 2013 08:00
Simple rack app
run proc{
res = "Hello World"
[
200,
{
"Connection" => "close",
"Access-Control-Allow-Origin" => [ "http://domain1.com", "http://domain2.com" ] * "\n",
"Content-Length" => res.bytesize.to_s
},
[res]
@fl00r
fl00r / gist:5955566
Last active December 19, 2015 12:29
require 'rack/utils'
run proc{
res = "Hello World"
headers = Rack::Utils::HeaderHash.new
headers["Connection"] = "close"
headers["Content-Length"] = res.bytesize.to_s
headers["Access-Control-Allow-Origin"] = [ "http://mir.mail.ru", "http://my.mail.ru" ]
[200, headers, [res] ]
@fl00r
fl00r / gist:7542994
Created November 19, 2013 09:56
Trie-like pure fuzzy search implementation on Ruby
class TrieDict
attr_reader :dict
def initialize
@dict = {}
end
def put(str)
d = nil
str.chars.each do |c|