Skip to content

Instantly share code, notes, and snippets.

;; example rails like console for compojure showing params, etc. for each request
(ns bus.core
(:use compojure.core
ring.adapter.jetty
...
...
(:use ring.middleware.params))
@svs
svs / gist:7fc59be7b4b0cc68b699
Last active August 1, 2019 10:39
Too Many `a`s (with apologies to Dr Seuss)
(Original here http://www.poetryfoundation.org/poem/171612)
Did I ever tell you that Programmer McCave
Had twenty-three variables and he named them all `a`
Well, he did. And that wasn't a smart thing to do.
You see, when he wants wants to add `a` to 2
He doesn't get a number oh no no no
All twenty-three `a`s cause a buffer overflow
#Given an array of numbers i.e. [1,2,3...1000], find out all possible combinations that add up to any given number i.e. 51437
#start with empty db
# start with 1st transaction -> write {key = 1, value => 1 }
# for the second transaction write { key => 1.2 => value => 3 }
# { key = 2, value => 3 }
# for the third transaction write { key => 1.2.3 => value => 6 }
# { key = 1.3, value => 4 }
# { key = 2.3, value => 5 }
# { key = 3, value => 3 }
# Assuming your execute command is more complex than a simple puts,
# you will need separate classes
class GoodbyeCommand
def execute
# complex logic here
end
end
class TwitterCommand
module GamesController
class Index < ControllerAction
def get
[200, {}, Game.all.to_json]
end
def params
super.except(:deleted_at) # no need for strong_params. specify what goes and what doesn't by overriding the params
@svs
svs / foos2.rb
Last active December 21, 2015 19:29
module FoosController
class Index < ControllerAction
def get
@foos = Foo.all
succeed_with(FooPresenter.new(@foos).to(format)) # if you want more control over presentation, hand off to the presenter
end
end # Index
class Create < ControllerAction
def post
class FoosController < ApplicationController
def index
###
end
def show
###
end
require 'active_support/core_ext/string/inflections'
class Router
attr_reader :env, :route
def initialize(routes, env)
@env = env
@routes = routes
end
class ChessApp
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw do
resources :games, :only => [:index, :create, :show, :update]
end
end
end
@svs
svs / gist:6354584
Created August 27, 2013 14:51
actiondispatch
@routes ||= ActionDispatch::Routing::RouteSet.new.tap do |r|
r.draw do
resources :games, :only => [:index, :create, :show, :update]
end
end
@routes.recognize_path("/games", {:method => 'GET'}) # => {:action => "index", :controller => "games"}