Skip to content

Instantly share code, notes, and snippets.

View danielnegri's full-sized avatar
Coffee first.

Daniel Negri danielnegri

Coffee first.
View GitHub Profile

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article < ActiveRecord::Base
require 'rubygems'
require 'bundler'
Bundler.require
require './application'
namespace :assets do
desc 'compile assets'
task :compile => [:compile_js, :compile_css] do
end
Services.factory("LawnchairFactory", function($window, $log, $parse) {
return function(name, config) {
var collection = {};
var array = [];
var isArray = config && config.isArray;
var idGetter = $parse((config && config.entryKey) ? config.entryKey : "id");
var transformSave = (config && config.transformSave) ? config.transformSave : angular.identity;
var transformLoad = (config && config.transformLoad) ? config.transformLoad : angular.identity;
function getEntryId(entry){
@danielnegri
danielnegri / dull_hamer_head.rb
Created August 4, 2014 21:08
Brute Force Decrypter
#!/usr/bin/env ruby
require 'digest/sha1'
class UnAuth
LIMIT = 10
SALT = ''
def initialize(encrypted_password)
@digits = [*('0'..'9'), *('a'..'z'), *('A'..'Z')]

Environment variables in Jekyll templates

This is one way to pass some data (API tokens, etc.) to your Jekyll templates without putting it in your _config.yml file (which is likely to be committed in your GitHub repository).

Copy the environment_variables.rb plugin to your _plugins folder, and add any environment variable you wish to have available on the site.config object.

In a Liquid template, that information will be available through the site object. For example, _layouts/default.html could contain:

@danielnegri
danielnegri / Vagrantfile
Created October 30, 2014 18:56
Vagrant File
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
WORKSPACE_ROOT = "/Users/grockit/workspace"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "saucy-i386"
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-i386-vagrant-disk1.box"
package com.scalapenos.spray
import spray.json._
/**
* A custom version of the Spray DefaultJsonProtocol with a modified field naming strategy
*/
trait SnakifiedSprayJsonSupport extends DefaultJsonProtocol {
import reflect._
@danielnegri
danielnegri / GoogleService.js
Created November 19, 2014 23:10
Titanium: Google Login
/*
*GoogleService
*
* CommonJS module for Google
* Uses OAuth2 for authentication/authorization
*
* Ayorinde Adesugba
* SISC
*/
@danielnegri
danielnegri / Vagrantfile
Created January 8, 2015 23:53
Vagrant Cluster - Master & Slaves
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
# Require a recent version of vagrant otherwise some have reported errors setting host names on boxes
Vagrant.require_version ">= 1.6.2"
INSTANCE_PREFIX="byoc"
@danielnegri
danielnegri / vpnsetup.sh
Last active August 29, 2015 14:13 — forked from hwdsl2/.MOVED.md
#!/bin/sh
#
# Amazon EC2 user-data file for automatic configuration of IPsec/L2TP VPN
# on a Ubuntu server instance. Tested with 14.04 (Trusty) AND 12.04 (Precise).
# With minor modifications, this script *can also be used* on dedicated servers
# or any KVM- or XEN-based Virtual Private Server (VPS) from other providers.
#
# DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC! THIS IS MEANT TO BE RUN WHEN
# YOUR AMAZON EC2 INSTANCE STARTS!
#