Skip to content

Instantly share code, notes, and snippets.

View PatrickLef's full-sized avatar

Patrick Lef PatrickLef

  • Looking for exciting projects!
  • Stockholm, Sweden
View GitHub Profile
##
## SERVER (config.ru)
## Start with: thin start -V -R config.ru
app = proc do |env|
[
200,
{
'Content-Type' => 'text/html',
'Content-Length' => '2'
#!/usr/bin/ruby
require 'rubygems'
gem 'sinatra', :version => "1.2.0"
require 'sinatra'
require 'mysql2/em'
gem "async_sinatra", :version => "0.5.0"
require 'sinatra/async'
require "sinatra/async/test"
require 'test/unit'
@PatrickLef
PatrickLef / gist:838858
Created February 22, 2011 15:45
ruby_monkeypatch
module Enumerable
# Convenient method instead of checking a.nil? || a.empty?
# Example:
# nil.emptil? ==> true
# [].emptil? ==> true
# {}.emptil? ==> true
# {:foo => 'bar'}.emptil? ==> false
# [1,2,3].emptil? ==> false
module ActiveResource
class Connection
private
# Handles response and error codes from the remote service.
def handle_response(response)
end
end
def provides(*types)
types = [types] unless types.kind_of? Array
types.map!{|t| mime_type(t)}
condition {
matching_types = (request.accept & types)
unless matching_types.empty?
response.headers['Content-Type'] = matching_types.first
true
else
Padrinotest.controller do
controller '/users', :provides => [:json] do
get '/' do
"foo"
end
end
end
def test_default_guest_gives_login_screen
get '/'
assert last_response.ok?
assert last_response.body.include?('log in to continue')
end
---------->
context "When accessing / guest should see the login screen" do
before :all do
# A user have methods for login and such
class User < Sequel::Model(:users)
end
# A administrator is a USER but have extra data in
# administrators table.
class Administrator < User
one_to_one :user
set_dataset(:administrators)
end
module Utils
# Creates a hash from instance vars
def attributes
vars = {}
instance_variables.each do |var|
vars[var.gsub("@", "").to_sym] = instance_variable_get(var)
end
vars
end
end
module Padrino
module Reloader
module Stat
class << self
def rotation
paths = Dir[Padrino.root("*")].unshift(Padrino.root).reject { |path| !File.directory?(path) }
files = paths.map { |path| Dir["#{path}/**/*.rb"] }.flatten.uniq
files.map{ |file|