Skip to content

Instantly share code, notes, and snippets.

@svs
svs / gist:6065669
Last active December 20, 2015 03:39
$git log --grep "help me"
(END)
$git log --grep "save me"
(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"}
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
require 'active_support/core_ext/string/inflections'
class Router
attr_reader :env, :route
def initialize(routes, env)
@env = env
@routes = routes
end
class FoosController < ApplicationController
def index
###
end
def show
###
end
@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
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
# 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
#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 }
# Given a table stat with primary key "slug" with data
# [{slug: 'foo', stats: {}}, {slug:'bar', stats:{}}]
updated_stats = {
'foo' => {a: 1, b: 2},
'bar' => {a: 3, b: 4}
}
# I can do this
updated_stats.each{|k,v|