Skip to content

Instantly share code, notes, and snippets.

View lucatironi's full-sized avatar

Luca Tironi lucatironi

View GitHub Profile
@lucatironi
lucatironi / fibonacci.rb
Created September 28, 2014 10:07
Fibonacci Comparison
# Naive slow implementation
def simple_fib(n)
return n if (0..1).include?(n)
simple_fib(n - 1) + simple_fib(n - 2)
end
# Recursive fast implementation
def rec_fib(n)
rec = -> (a, b, n) { n == 0 ? a : rec.call(b, a + b, n - 1) }
rec.call(0, 1, n)
@lucatironi
lucatironi / retrieve_path_in_hash.rb
Created November 15, 2014 09:11
Retrieve a path in an Hash
def retrieve(path, hash)
remainder = hash.fetch(path.shift, nil)
remainder.is_a?(Hash) ? retrieve(path, remainder) : remainder
end
path = [:foo, :bar, :baz]
hash = { foo: { bar: { baz: 123 } } }
puts "path: #{path}"
puts "hash: #{hash}"
@lucatironi
lucatironi / gist:1339779
Created November 4, 2011 16:33
state_machine
# GemFile
gem 'state_machine'
# offerta.rb
scope :nuove, Offerta.where(:stato => "nuova")
STATI = %w(nuova inviata accettata rifiutata scaduta annullata)
state_machine :stato, :initial => :nuova do
before_transition any => :inviata do |offerta, transition|
@lucatironi
lucatironi / index.xml.builder
Created December 2, 2011 15:05
Sitemap.xml for a Rails App for Google Webmasters Tools
# app/views/sitemap/index.xml.builder
xml.instruct!
xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
xml.url do
xml.loc root_url
xml.priority 1.0
end
xml.url do
@lucatironi
lucatironi / categories_routing_spec.rb
Created December 2, 2011 15:22
Github's Style Routes
require 'spec_helper'
describe CategoriesController do
describe "routing" do
it '/solutions to Category#index' do
path = categories_path
path.should == '/solutions'
{ :get => path }.should route_to(
:controller => 'categories',
:action => 'index'
@lucatironi
lucatironi / order.rb
Created December 2, 2011 16:28
Order Placement Wizard for a services e-commerce
class Order < ActiveRecord::Base
extend FriendlyId
belongs_to :customer
belongs_to :service
belongs_to :request_address, :foreign_key => "request_address_id", :class_name => "Address"
belongs_to :billing_address, :foreign_key => "billing_address_id", :class_name => "Address"
has_many :options, :through => :choices, :autosave => true
has_many :choices, :dependent => :destroy
@lucatironi
lucatironi / gist:3899430
Created October 16, 2012 13:55
Fixed scrolling sidebar
// Keeps the order recap box fixed on scroll, a la Apple Store
// http://jqueryfordesigners.com/fixed-floating-elements
$(function () {
var msie6 = $.browser == 'msie' && $.browser.version < 7;
if (!msie6 && $('#sidebar').length > 0 && false) {
var top = $('#logo').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0)),
order_recap_height = $('#sidebar').height(),
order_form_wrapper_height = $('#main-content').height();
class ApplicationController < ActionController::Base
before_filter :set_locale
protect_from_forgery
def set_locale
logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
I18n.locale = current_user ? current_user.locale : (params[:locale] || extract_locale_from_accept_language_header || I18n.default_locale)
logger.debug "* Locale set to '#{I18n.locale}'"
end
require 'spec_helper'
describe Form do
context "ActiveRecord" do
it { should belong_to(:user) }
it { should have_many(:entries) }
it { should validate_presence_of(:mail_to_address) }
it { should validate_presence_of(:redirect_to_url) }
end
package net.primegap.authexample;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.BasicResponseHandler;