Skip to content

Instantly share code, notes, and snippets.

class Array
def to_proc
m, args = self
lambda {|x| x.send(m.to_sym, *args) }
end
end
p [{:a => 1}].find(&[:key?, :a]) #=> {:a => 1}
p [{:a => 1}].find(&[:key?, :b]) #=> nil
# XXX more specific name.
class Person < ActiveRecord::Base
exec_context :hoge, default: true do
validate :foo, presence: true
before_create :foobar
end
validate :bar, presence: true
end
@moro
moro / gist:1258273
Created October 3, 2011 01:50
detect merged repos
#!/usr/bin/env ruby
remote = 'origin'
merged = Dir.glob("./.git/logs/refs/remotes/#{remote}/*").inject([]) do |merged, ref|
branch = File.basename(ref)
unmerged = `git log #{remote}/master..#{remote}/#{branch}`
unmerged.empty? ? merged << branch : merged
end
@moro
moro / gist:1202952
Created September 8, 2011 08:44
Nokogiri+grep
#!/usr/bin/env ruby
require 'nokogiri'
require 'optparse'
$optval = {}
opt = OptionParser.new
opt.on('-a', '--attr=ATTR', 'display attribute value') {|v| $optval[:attr] = v }
opt.on('-i', '--inspect', 'output #inspect instead of #to_s') { $optval[:inspect] = true }
@moro
moro / gist:1179935
Created August 30, 2011 01:39
my ~/.rspec
--color
--profile
<%= begin; require 'ruby-debug'; Debugger::Command.settings[:autolist] = Debugger::Command.settings[:autoeval] = 1 ; '--debugger' ; rescue LoadError ; '' ; end %>
# vim:set ft=zsh:
unlimit
limit core 0
limit -s
umask 022
HISTFILE=${HOME}/.zhistory
HISTSIZE=100000
SAVEHIST=100000
From 61b68702748ff7da4fbc97a2bdf00c39a71e63ab Mon Sep 17 00:00:00 2001
From: moro <[email protected]>
Date: Thu, 1 Oct 2009 20:58:39 +0900
Subject: [PATCH] impl. rack middleware to block invalid chars.
block invalid chars, which is redundant UTF-8 chars for example,
to come into Rails app world.
---
actionpack/lib/action_controller.rb | 1 +
.../lib/action_controller/invalid_char_blocker.rb | 49 +++++++++
@moro
moro / gist:1028677
Created June 16, 2011 04:35
samp.ru
require 'rack'
class App
def call(env)
to = Rack::Request.new(env).params["to"] || "World"
body = Rack::Utils.escape_html(to)
[200, {"Content-Type" => "text/html"}, [body]]
end
end
@moro
moro / gist:1024620
Created June 14, 2011 10:05
Kaminari and pushState
(function($){
$('.pagination a[data-remote=true]').live('ajax:success', function(e){ window.history.pushState('', '', $(e.target).attr('href')) })
$(window).bind('popstate', function(){ $.ajax({url:window.location, dataType:'script'}) ; return true });
})(jQuery);
@moro
moro / gist:1009814
Created June 6, 2011 06:11
define column default in app/models/*.rb
class Person < ActiveRecord::Base
def self.column_default_values(defs)
defs.each do |cname, value|
columns.detect{|c| c.name.to_sym == cname.to_sym }.instance_variable_set('@default', value)
end
end
column_default_values(name: 'hibariya')
end