Skip to content

Instantly share code, notes, and snippets.

@brooklynDev
brooklynDev / erb_compile
Last active August 29, 2015 14:06
ERB compile one liner
require 'erb'
require 'ostruct'
class << ERB
def compile(template, locals)
renderer = ERB.new(template)
struct = OpenStruct.new(locals)
class << struct
def get_binding
binding()
@brooklynDev
brooklynDev / Express JsonFlash
Last active August 29, 2015 14:01
Out of the box, express-flash only supports strings, occasionally it's useful to have some json stick around after redirects. This middleware tacks on a jsonFlash function to accomplish that. If you require this middleware you have both flash() and jsonFlash();
var flash = require('express-flash')();
module.exports = function() {
return function(req, res, next) {
flash(req, res, function() {
req.jsonFlash = function() {
if (arguments.length === 1) {
var data = req.flash(arguments[0]);
if (data.length) {
return JSON.parse(data);
@brooklynDev
brooklynDev / gist:3893421
Created October 15, 2012 16:29
Asynchronouse file download with WebRequest + show progress in Progress bar. APM vs async/await
#######################################
APM
#######################################
public partial class APMVersion : Form
{
private const int BUFFER_SIZE = 1024;
public Form1()
{
@brooklynDev
brooklynDev / gist:3185573
Created July 27, 2012 01:12
Express routes/controller initialization
//app.js
var express = require('express')
, http = require('http')
, fs = require('fs');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
var random = new Random();
RandomStreamGenerator.GenerateIndefinitley(() => (Keys)(random.Next(1,150), k => /* do something with the key */);
public static class RandomStreamGenerator
{
private static Random _random = new Random();
public static void GenerateIndefinitley<T>(Func<T> func, Action<T> onNext)
{
GenerateIndefinitley(func, 100, 1000, onNext);
}
public static void GenerateIndefinitley<T>(Func<T> func, int minMilliseconds, int maxMilliseconds, Action<T> onNext)
<script type="text/javascript">window.ngon={};ngon.Person={"FirstName":"John","LastName":"Doe","Age":30};</script>
public static class HtmlHelperExtensions
{
public static IHtmlString IncludeNGon(this HtmlHelper helper, string @namespace = "ngon")
{
var viewData = helper.ViewContext.ViewData;
if (viewData == null)
{
return MvcHtmlString.Empty;
}
public class NGonActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.NGon = new ExpandoObject();
base.OnActionExecuting(filterContext);
}
}
<script type="text/javascript">
$(function () {
$("#button").click(function () {
var person = ngon.Person;
var div = $("#output");
div.html('');
div.append("FirstName: " + person.FirstName);
div.append(", LastName: " + person.LastName);
div.append(", Age: " + person.Age);
});