Skip to content

Instantly share code, notes, and snippets.

View oleganza's full-sized avatar

Oleg Andreev oleganza

View GitHub Profile
// BNF parser framework for JavaScript
// Oleg Andreev
var result = (function(text){
// The Y Combinator
var Y=function (gen) {
return function(f) {return f(f)}(
function(f) {
return gen(function() {return f(f).apply(null, arguments)})})}
# I wonder why we still use File instance monkey-patching
# instead of a regular subclass/delegator?
# Every time you test uploaded files, you come up with a brand-new mock.
# Duh...
class UploadedFile < Delegator
attr_accessor :file, :mime_type, :original_filename
def initialize(file, mime_type, original_filename)
super(file)
@file = file
# Simple cache implementation for DB supporting simple collection retrieval
# Static types (i.e. tables) are omitted for brevity.
class Repository
attr_accessor :object_cache, :collection_cache, :backend
def get(id)
get_many([id]).first
end
module Id64 extend self
Alphabet = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_-"
(Alphabet.size == 64) or raise "Alphabet should have 64 characters!"
def random
Array.new(21).map{ Alphabet[rand(2**6),1] }.join
end
end
# Oleg Andreev <oleganza@gmail.com> Oct 16, 2009
#
# This demonstrates how to perform a request to the same Rails instance without hitting HTTP server.
# Note 1: this does not boot another Rails instance
# Note 2: this does not involve HTTP parsing
# Note 3: dispatch_unlocked and rack.multithread=true are used to prevent deadlock on a dispatch mutex,
# NOT to make inner requests operate concurrently as one may think.
# Note 4: inner request is created by merging outer request with some HTTP headers and rack options.
# This may probably lead to strange effects, so be aware of it.
# Perhaps, we shouldn't use outer request at all. I don't know.
class BlankSlate
class <<self; alias __undef_method undef_method; end
alias __instance_eval instance_eval
ancestors.inject([]){|m,a| m + a.methods }.uniq.
each { |m| (__undef_method(m) rescue nil) unless m =~ /^__/ }
end
class CSSBuilder < BlankSlate
class ::Numeric
require 'strscan'
require 'cgi'
module HTMLEscaper
ENTITIES = {
"<" => "&lt;",
">" => "&gt;",
'"' => "&quot;",
'&' => "&amp;"
}
module Id64 extend self
Alphabet = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_-"
(Alphabet.size == 64) or raise "Alphabet should have 64 characters!"
def random(length = 22)
Array.new(length).map{ Alphabet[rand(2**6),1] }.join # 64**22 == 2**132
end
end
def smallAsciiRepresentation(digest)
digestParts = digest.unpack("Q2") # digest is a 128 bit buffer, cut it into two 64 bits ruby Integer
smallDigestPart = digestParts[0] ^ digestParts[1] # XOR the two 64 bits parts
smallDigest = [smallDigestPart].pack("Q") # and transform the result into a 64 bits binary buffer
smallDigestB64 = [smallDigest].pack("m") # b64-encode the result
smallID = smallDigestB64.gsub(/[\n=]/, '').gsub(/\+/,'-').gsub(/\//,'_') # and make it URL-friendly by shortening it (no '=' at the end, no '+', no '/')
return smallID
end
- (void) updateToolOptionsView
{
ToolOptionsViewController* optionsController = tool.optionsViewController;
// Stretch view frame according to autoresizing rules
// If the view is not stretched, there will be autosurprising
NSView* optionsView = [optionsController view];
NSSize optionsSize = [optionsView frame].size;
NSSize targetSize = [[self.scrollView contentView] frame].size;
NSSize newOptionsSize = optionsSize;