Skip to content

Instantly share code, notes, and snippets.

View jeffreyiacono's full-sized avatar

Jeff Iacono jeffreyiacono

  • Sierra.ai
  • San Francisco
  • X @jfi
View GitHub Profile
@jeffreyiacono
jeffreyiacono / helpers.js
Created December 20, 2011 20:59
another little javascript helper
var ApplicationHelpers = {
/**
* Note: requires Underscore.js
*
* Enable a (sencha touch) form to have a link between
* model and form (ie. MyModel.some_attribute <=> textfield.some_attribute)
* AND allow for POST / PUT submission in the rails-friendly namespaced format
* (ie. {my_model[some_attribute] : "some value"}).
*
* To get above behavior, treate model and forms in normal fashion and use
@jeffreyiacono
jeffreyiacono / sencha_touch_app_helpers.js
Created December 21, 2011 17:49
js helper to "railsify" param keys so sencha touch 2 and rails controllers play nice
/**
* Application helpers
* requires Underscore.js
*/
var ApplicationHelpers = {
/**
* Railsify object's keys to play nice with default Rails controller setup.
* Rails default controller setup expects params submitted via PUT / POST as:
*
* (POST) {'my_model[attr1]' : 'value1', ... etc. }
@jeffreyiacono
jeffreyiacono / sencha.rb
Created December 27, 2011 06:41
module that generates json for usage by sencha touch 2 nested list menu component
module Sencha
module NestedListMenu
class << self
# Sencha Touch nested list menu requires json in the form of:
#
# {"text": "some identifier",
# "children": ["text": "child identifier", "other": "info", "leaf": "true"],
# [ ... ]
# },
# { ... }
@jeffreyiacono
jeffreyiacono / ruby-splat.rb
Created January 6, 2012 22:58
fun with splat in ruby
def add(a, b)
a + b
end
def say(what, *people)
people.each { |person| puts "#{person}: #{what}" }
end
say "Hello!", "Bob", "Alice"
# Bob: Hello!
@jeffreyiacono
jeffreyiacono / pace_car.js
Created January 25, 2012 18:41
a simple pace car to test how fast (or slow) your js runs
/**
* PaceCar makes it easy to test how fast (or slow) you js runs
*
* Example Usage:
*
* // get a PaceCar ready
* var p = new PaceCar();
* // start your engines
* p.start();
* // some sample code you want to test performance of.
@jeffreyiacono
jeffreyiacono / config.ru
Created January 28, 2012 23:14
sample sinatra config.ru that mounts multiple apps
require 'rubygems'
require 'bundler'
Bundler.require
require './app1'
require './app2'
map '/app1' do
run App1
@jeffreyiacono
jeffreyiacono / make_time_agoable.rb
Created February 2, 2012 00:33
module that enables a model to produce "time ago" string representations for any given date field (requires active_support)
module MakeTimeAgoable
extend ActiveSupport::Concern
included do
# code goes here that should be run on the actual include
# or just leave it blank. no one cares. where's my cake?
end
module ClassMethods
attr_reader :_time_agoable
@jeffreyiacono
jeffreyiacono / mongoid_counter_cache.rb
Created February 4, 2012 22:52
counter cache extension for mongoid orm
module Mongoid
module CounterCache
extend ActiveSupport::Concern
module ClassMethods
# Use Case:
#
# keep a cached count of embedded collections in the parent collection(s)
#
# for example, if a Post embeds many Comments, and there have been 2
@jeffreyiacono
jeffreyiacono / Rakefile
Created February 8, 2012 20:15
rake task for precompiling assets using sprockets within a sinatra app + view helpers
require 'rubygems'
require 'bundler'
Bundler.require
require './application'
namespace :assets do
desc 'compile assets'
task :compile => [:compile_js, :compile_css] do
end
@jeffreyiacono
jeffreyiacono / family-sim.rb
Created February 11, 2012 02:12
simulation of the math problem: "In a country in which people only want boys, every family continues to have children until they have a boy. If they have a girl, they have another child. If they have a boy, they stop. What is the proportion of boys to gir
# simulation to calculate an answer to the question:
#
# "In a country in which people only want boys, every family continues to have children until they have a boy. If they
# have a girl, they have another child. If they have a boy, they stop. What is the proportion of boys to girls in the
# country?"
#
# !/usr/bin/env ruby
# usage: ./family-sim.rb [trials]
#
# sample output: