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
@danielnegri
danielnegri / converter.rb
Created January 31, 2014 20:00
Migrate Jasper XML to Sqlite3
require 'nokogiri'
require 'sqlite3'
class Converter
def initialize(file, database_name = "flashcards")
@file = file
@database_name = database_name
@database = open_database()
end
@danielnegri
danielnegri / publigrations_controller.rb
Created February 19, 2014 00:18
Sample: Publish and Migrate
class PubligrationsController < ApplicationController
before_filter :admin_required
def index
render widget: Views::Publigrations::Index, user: current_user
end
def publish
messages = []
unless subtrack = Subtrack.find_by_id(params[:subtrack_id], include: {question_sets: :questions})

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 &lt; 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
*/