Skip to content

Instantly share code, notes, and snippets.

@janx
janx / hash_patch.rb
Created May 7, 2013 12:52
Monkey patch hash so it behaves like OpenStruct
class Hash
alias :__values :values
def values
self["values"] || __values
end
alias :__method_missing :method_missing
def method_missing(name, *args, &block)
if args.empty?
self[name.to_s] || self[name]
@janx
janx / gc_middleware.rb
Created May 2, 2013 11:02
GC trick, reduce gc frequency by time factor, create hiccups in response time
class GCMiddleware
def initialize(app, options={})
@app = app
@options = options
end
def call(env)
begin_gc_deferment
result = @app.call(env)
reconsider_gc_deferment(env)
@janx
janx / Problem when run 'rake'
Created November 15, 2012 06:22
setup patch
14:23 ~/projects/work/rentals master rake
(in /home/jan/projects/work/rentals)
warning: metric_fu gem desired but not found.
/home/jan/.rvm/gems/ruby-1.8.7-p352/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning: Gem::Dependency#version_requirements is deprecated and will be removed on or after August 2010. Use #requirement
/home/jan/projects/work/rentals/lib/endeca/dimension.rb:3: undefined method `reader' for Endeca::Dimension:Class (NoMethodError)
from /home/jan/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:158:in `require'
from /home/jan/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:158:in `require'
from /home/jan/projects/work/rentals/config/initializers/requires.rb:1
from /home/jan/projects/work/rentals/config/initializers/requires.rb:1:in `each'
from /home/jan/projects/work/rentals/config/initializers/requires.rb:1
@janx
janx / refactor.patch
Created February 10, 2012 02:01
refactoring of Membership create
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 5747a13..c5c9a05 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -12,8 +12,7 @@ class Admin::UsersController < AdminController
@email = Email.new(email: @identity.email, confirmed: true)
@user = User.create!(name: @identity.name, nickname: @identity.nickname,
emails: [@email])
- # create the membership for other domain user.
- Membership.create!(:account => account, :email => @email) unless account.membership_for(@user)
@janx
janx / xmonad.hs
Created December 24, 2011 12:00
my xmonad config
import XMonad hiding ( (|||) )
import XMonad.Actions.CopyWindow
import XMonad.Actions.CycleWS
import XMonad.Actions.NoBorders
import XMonad.Actions.Submap
import XMonad.Actions.Search
import XMonad.Actions.WindowBringer (bringWindow)
import XMonad.Config.Gnome
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ManageHelpers
@janx
janx / passenger-httpd.spec
Created December 15, 2011 09:09
passenger-httpd.spec
Summary: Rubygem Passenger and its Apache2 module
Name: passenger-httpd
Version: 3.0.11
Release: 1
Vendor: Socialspring, Inc
Group: System Environment/Daemons
License: Modified BSD
URL: http://www.modrails.com/
Source: http://rubyforge.org/frs/download.php/75548/passenger-3.0.11.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot-%(%{__id_u} -n)
@janx
janx / analog_clock.html
Created September 2, 2011 13:04
html5 svg analog clock
<!DOCTYPE HTML>
<html>
<head>
<title>Analog Clock</title>
<script>
function updateTime() { // Update the SVG clock
var now = new Date();
var sec = now.getSeconds();
var min = now.getMinutes();
var hour = (now.getHours() % 12) + min/60;
@janx
janx / fetchsub.py
Created July 27, 2011 02:31
Feching subtitles on shooter.cn
#!/usr/bin/python2
USAGESTR = """Usage: fetchsub.py [-l langcode] videofile1 [videofile2 ...]
langcode: chn [default]
eng
sub file will saved in the same directory of each videofile
XXXXXXX.mkv 's sub files will named as:
XXXXXXX.chn.0.srt, XXXXXXX.chn.1.srt, XXXXXXX.chn.2.srt
"""
"""Author: sevenever
@janx
janx / gist:771546
Created January 9, 2011 08:53
redirect POST
# RFC2616 10.3.3
# If the 302 status code is received in response to a request other
# than GET or HEAD, the user agent MUST NOT automatically redirect the
# request unless it can be confirmed by the user, since this might
# change the conditions under which the request was issued.
# Note: RFC 1945 and RFC 2068 specify that the client is not allowed
# to change the method on the redirected request. However, most
# existing user agent implementations treat 302 as if it were a 303
# response, performing a GET on the Location field-value regardless
class A
def method_missing(m, *args)
n = m.to_s.split('_')[1].to_i
send "print_#{n-1}"
puts n
end
def print_1
puts 1
end