Skip to content

Instantly share code, notes, and snippets.

View realmyst's full-sized avatar

Alexander Shcherbinin realmyst

  • Russia, Ulyanovsk
View GitHub Profile
def normalize_params(params, name, v = nil)
name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
k = $1 || ''
after = $' || ''
return if k.empty?
if after == ""
params[k] = v
elsif after == "[]"
@realmyst
realmyst / gist:3340984
Created August 13, 2012 13:53 — forked from kosmatov/image-preview-html5.js
File preview on HTML5/JS/jQuery
$('form').on('change', 'input[type="file"]', function(e) {
var files = e.target.files,
f = files[0],
reader = new FileReader(),
t = this
;
reader.onload = (function(file) {
@realmyst
realmyst / polish.rb
Created July 18, 2012 13:05
Reverse Polish Notation, first try
#
# Requirement: delimiter ',' after each operands
# Example:
#
# ~> ruby polish '8,2,5,*+1,3,2,*+4,-/'
# result: 6
#
class ReversePolishTypeError < StandardError; end
@realmyst
realmyst / gist:2366995
Created April 12, 2012 12:45
rails pluralization
#yml:
in_count_complex:
one: "в %{count} комплексе"
few: "в %{count} комплексах"
many: "в %{count} комплексах"
#view:
= t :in_count_complex, :count => projects.count
require 'formula'
class Vim <Formula
url 'ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2'
homepage 'http://www.vim.org/'
md5 '5b9510a17074e2b37d8bb38ae09edbf2'
version '7.3.135'
def patchlevel; 135 end
def features; %w(tiny small normal big huge) end
@realmyst
realmyst / gist:1809125
Created February 12, 2012 15:48
acl test example
it 'should be work with asserts' do
acl = YaAcl::Builder.build do
roles do
role :admin
role :another_user
role :editor
role :operator
end
asserts do
@realmyst
realmyst / authorization_test_helper.rb
Created February 1, 2012 13:29
Helper for testing ACL
module AuthorizationTestHelper
#helper module
module AuthenticatedTestHelper
include AuthHelper
def login_as(role_or_user = :admin, role_sym = :admin)
if role_or_user.kind_of? model_user
user = role_or_user
else
@realmyst
realmyst / gist:1511738
Created December 22, 2011 20:31
rvm zshrc
# system
[[ -s '/usr/local/lib/rvm' ]] && source '/usr/local/lib/rvm'
# single
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
@realmyst
realmyst / gist:1434209
Created December 5, 2011 16:35
capistrano deploy notifications with ubuntu libnotify
# put this code at ~/.caprc
# install libnotify-bin
# run deploy
# profit :)
def notify(message, body, urgency, icon = :info)
system("notify-send --urgency=#{urgency} --icon=#{icon} '#{message}' '#{body}'")
end
on :exit do
@realmyst
realmyst / gist:1262561
Created October 4, 2011 19:34
Склонение числительных в javascript
function declOfNum(number, titles) {
cases = [2, 0, 1, 1, 1, 2];
return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];
}
use:
declOfNum(count, ['найдена', 'найдено', 'найдены']);